/// <summary> /// Get free space for a partition /// </summary> /// <param name="inp">The DeviceID for the partition</param> /// <param name="partition">Reference to the partition instance</param> private void GetFreeSpace(string inp, ref HDDPartition partition) { ManagementObjectSearcher getspace = new ManagementObjectSearcher("Select * from Win32_LogicalDisk Where DeviceID='" + inp + "'"); foreach (ManagementObject drive in getspace.Get()) { if (drive["DeviceID"].ToString() == inp) { partition.FreeSpace = Convert.ToUInt64(drive["FreeSpace"]); partition.TotalSpace = Convert.ToUInt64(drive["Size"]); partition.FreeSapceInPercent = partition.FreeSpace * 100 / partition.TotalSpace; partition.FileSystem = Convert.ToString(drive["FileSystem"]); } } }
/// <summary> /// Get HDD SMART Information for installed drives /// Initial version from here: http://www.know24.net/blog/C+WMI+HDD+SMART+Information.aspx /// </summary> /// <returns></returns> public Dictionary<int, HDD> GetHddSmartInformation() { // retrieve list of drives on computer (this will return both HDD's and CDROM's and Virtual CDROM's) var result = new Dictionary<int, HDD>(); try { var wdSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); // extract model and interface information int iDriveIndex = 0; foreach (ManagementObject drive in wdSearcher.Get()) { var hdd = new HDD(); hdd.Model = drive["Model"].ToString().Trim(); hdd.Type = drive["InterfaceType"].ToString().Trim(); hdd.Firmware = drive["FirmwareRevision"].ToString().Trim(); hdd.TotalSize = Convert.ToUInt64(drive["Size"]); result.Add(iDriveIndex, hdd); iDriveIndex++; // Get Partitions string DiskName = "Disk " + drive["Index"].ToString() + " : " + drive["Caption"].ToString().Replace(" ATA Device", "") + " (" + Math.Round(Convert.ToDouble(drive["Size"]) / 1073741824, 1) + " GB)"; int ObjCount = Convert.ToInt16(drive["Partitions"]); ManagementObjectSearcher partitions = new ManagementObjectSearcher("Select * From Win32_DiskPartition Where DiskIndex='" + drive["Index"].ToString() + "'"); foreach (ManagementObject part in partitions.Get()) { HDDPartition p = new HDDPartition(); p.PartitionName = "Partition " + part["Index"].ToString(); string PartName = part["DeviceID"].ToString(); if (part["Bootable"].ToString() == "True" && part["BootPartition"].ToString() == "True") { p.DiskName = "Recovery"; p.TotalSpace = Convert.ToUInt64(part["Size"]); } else { ManagementObjectSearcher getdisks = new ManagementObjectSearcher("Select * From Win32_LogicalDiskToPartition Where "); p.DriveLetter = this.GetPartitionName(PartName); GetFreeSpace(p.DriveLetter, ref p); p.DiskName = "Local Disk (" + p.DriveLetter + ")"; } hdd.Partitions.Add(p); } } var pmsearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); // retrieve hdd serial number iDriveIndex = 0; foreach (ManagementObject drive in pmsearcher.Get()) { // because all physical media will be returned we need to exit // after the hard drives serial info is extracted if (iDriveIndex >= result.Count) break; result[iDriveIndex].Serial = drive["SerialNumber"] == null ? "None" : drive["SerialNumber"].ToString().Trim(); iDriveIndex++; } // get wmi access to hdd var searcher = new ManagementObjectSearcher("Select * from Win32_DiskDrive"); searcher.Scope = new ManagementScope(@"\root\wmi"); // check if SMART reports the drive is failing searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictStatus"); iDriveIndex = 0; foreach (ManagementObject drive in searcher.Get()) { result[iDriveIndex].IsOK = (bool)drive.Properties["PredictFailure"].Value == false; iDriveIndex++; } // retrive attribute flags, value worste and vendor data information // MSStorageDriver_ATAPISmartData // MSStorageDriver_FailurePredictData searcher.Query = new ObjectQuery("Select * from MSStorageDriver_ATAPISmartData"); iDriveIndex = 0; foreach (ManagementObject data in searcher.Get()) { Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value; for (int i = 0; i < 30; ++i) { try { int id = bytes[i * 12 + 2]; int flags = bytes[i * 12 + 4]; // least significant status byte, +3 most significant byte, but not used so ignored. //bool advisory = (flags & 0x1) == 0x0; bool failureImminent = (flags & 0x1) == 0x1; //bool onlineDataCollection = (flags & 0x2) == 0x2; int value = bytes[i * 12 + 5]; int worst = bytes[i * 12 + 6]; int vendordata = BitConverter.ToInt32(bytes, i * 12 + 7); if (id == 0) continue; var attr = result[iDriveIndex].Attributes[id]; attr.Current = value; attr.Worst = worst; attr.Data = vendordata; attr.IsOK = failureImminent == false; } catch { // given key does not exist in attribute collection (attribute not in the dictionary of attributes) } } iDriveIndex++; } // retreive threshold values foreach attribute searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictThresholds"); iDriveIndex = 0; foreach (ManagementObject data in searcher.Get()) { Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value; for (int i = 0; i < 30; ++i) { try { int id = bytes[i * 12 + 2]; int thresh = bytes[i * 12 + 3]; if (id == 0) continue; var attr = result[iDriveIndex].Attributes[id]; attr.Threshold = thresh; } catch { // given key does not exist in attribute collection (attribute not in the dictionary of attributes) } } iDriveIndex++; } } catch (ManagementException ex) { // Log-Exception DependencyFactory.Resolve<ILoggingService>(ServiceNames.LoggingService).LogException("WmiHardwareInfoService: Fehler bei der Ermittlung der S.M.A.R.T-Informationen", ex); // Show exception DependencyFactory.Resolve<IExceptionReporterService>(ServiceNames.ExceptionReporterService).ReportException(ex); } return result; }