/// <summary> /// Searches disk information by WMI (Win32_DiskDrive, Win32_DiskPartition, Win32_LogicalDisk). /// </summary> /// <returns>Enumeration of disk information</returns> private static IEnumerable <DiskInfo> SearchByDiskDrive() { using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")) { foreach (ManagementObject disk in searcher.Get()) // Casting to ManagementObject is for GetRelated method. { if (!(disk["Index"] is uint)) // Index represents index number of physical drive. { continue; } var info = new DiskInfo(); info.PhysicalDrive = (uint)disk["Index"]; info.MediaType = disk["MediaType"] as string; info.Size = (disk["Size"] is ulong) ? (ulong)disk["Size"] : 0L; var driveLetters = new List <string>(); foreach (ManagementObject diskPartition in disk.GetRelated("Win32_DiskPartition")) // Casting to ManagementObject is for GetRelated method. { if (!(diskPartition["DiskIndex"] is uint) || // DiskIndex represents index number of physical drive. ((uint)diskPartition["DiskIndex"] != info.PhysicalDrive)) { continue; } foreach (var logicalDisk in diskPartition.GetRelated("Win32_LogicalDisk")) { var driveLetter = logicalDisk["DeviceID"] as string; // Drive letter if (!string.IsNullOrWhiteSpace(driveLetter) && driveLetter.Trim().EndsWith(':'.ToString(CultureInfo.InvariantCulture))) { driveLetters.Add(driveLetter.Trim()); } if ((info.DriveType == 0) && (logicalDisk["DriveType"] is uint)) { info.DriveType = (uint)logicalDisk["DriveType"]; } } } if (!driveLetters.Any()) { continue; } info.DriveLetters = driveLetters.ToArray(); yield return(info); } } }
/// <summary> /// Search drives by WMI (Win32_DiskDrive, Win32_DiskPartition, Win32_LogicalDisk). /// </summary> private static void SearchDiskDrive(ref List <DiskInfo> diskGroup) { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); foreach (var drive in searcher.Get()) { if (drive["Index"] == null) // Index number of physical drive { continue; } int index; if (!int.TryParse(drive["Index"].ToString(), out index)) { continue; } var info = new DiskInfo(); info.PhysicalDrive = index; if (drive["MediaType"] != null) { info.MediaType = drive["MediaType"].ToString(); } if (drive["Size"] != null) { ulong numSize; if (ulong.TryParse(drive["Size"].ToString(), out numSize)) { info.Size = numSize; } } var driveLetters = new List <string>(); foreach (var diskPartition in ((ManagementObject)drive).GetRelated("Win32_DiskPartition")) { if ((diskPartition["DiskIndex"] == null) || // Index number of physical drive (diskPartition["DiskIndex"].ToString() != info.PhysicalDrive.ToString(CultureInfo.InvariantCulture))) { continue; } foreach (var logicalDisk in ((ManagementObject)diskPartition).GetRelated("Win32_LogicalDisk")) { if (logicalDisk["DeviceID"] == null) // Drive letter { continue; } var driveLetter = logicalDisk["DeviceID"].ToString().Trim(); if (String.IsNullOrEmpty(driveLetter) || !driveLetter.EndsWith(':'.ToString(CultureInfo.InvariantCulture))) { continue; } driveLetters.Add(driveLetter); if (logicalDisk["DriveType"] != null) { uint numType; if (!uint.TryParse(logicalDisk["DriveType"].ToString(), out numType)) { continue; } info.DriveType = numType; } } } if (!driveLetters.Any()) { continue; } info.DriveLetters = driveLetters.ToArray(); diskGroup.Add(info); } }
/// <summary> /// Read Config file. /// </summary> /// <remarks>True if completed</remarks> internal async Task<bool> ReadAsync(DiskInfo info) { if (info == null) throw new ArgumentNullException("info"); AssociatedDisk = info; var configPath = ComposeConfigPath(); if (!File.Exists(configPath)) return false; try { using (var sr = new StreamReader(configPath, Encoding.ASCII)) { Import(await sr.ReadToEndAsync()); return true; } } catch (Exception ex) { Debug.WriteLine("Failed to read config file. {0}", ex); return false; } }
/// <summary> /// Search drives by WMI (Win32_DiskDrive, Win32_DiskPartition, Win32_LogicalDisk). /// </summary> /// <param name="diskGroup">List of disk information</param> private static void SearchDiskDrive(ref List<DiskInfo> diskGroup) { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); foreach (var drive in searcher.Get()) { if (drive["Index"] == null) // Index number of physical drive continue; int index; if (!int.TryParse(drive["Index"].ToString(), out index)) continue; var info = new DiskInfo(); info.PhysicalDrive = index; if (drive["MediaType"] != null) { info.MediaType = drive["MediaType"].ToString(); } if (drive["Size"] != null) { ulong numSize; if (ulong.TryParse(drive["Size"].ToString(), out numSize)) info.Size = numSize; } var driveLetters = new List<string>(); foreach (var diskPartition in ((ManagementObject)drive).GetRelated("Win32_DiskPartition")) { if ((diskPartition["DiskIndex"] == null) || // Index number of physical drive (diskPartition["DiskIndex"].ToString() != info.PhysicalDrive.ToString(CultureInfo.InvariantCulture))) continue; foreach (var logicalDisk in ((ManagementObject)diskPartition).GetRelated("Win32_LogicalDisk")) { if (logicalDisk["DeviceID"] == null) // Drive letter continue; var driveLetter = logicalDisk["DeviceID"].ToString().Trim(); if (String.IsNullOrEmpty(driveLetter) || !driveLetter.EndsWith(':'.ToString(CultureInfo.InvariantCulture))) continue; driveLetters.Add(driveLetter); if (logicalDisk["DriveType"] != null) { uint numType; if (!uint.TryParse(logicalDisk["DriveType"].ToString(), out numType)) continue; info.DriveType = numType; } } } if (!driveLetters.Any()) continue; info.DriveLetters = driveLetters.ToArray(); diskGroup.Add(info); } }