windows 如何使用C#读取扩展的智能数据?

holgip5t  于 2023-11-21  发布在  Windows
关注(0)|答案(1)|浏览(189)

我知道一点C++和Java,但我想自己学习C#.为了搞砸,我试图读取我的硬盘驱动器的SMART数据.我有这个C#代码,但我不知道如何修改它读取额外的内存值:它显然读取的是“Value”值,而不是“Worst”或“Threshold”值。(最差和阈值)到程序。弄清楚如何做到这一点将有助于我学习C#一点。
C#示例:(我想使用的)

  1. // (c) Microsoft Corporation
  2. // Author: Clemens Vasters ([email protected])
  3. // Code subject to MS-PL: http://opensource.org/licenses/ms-pl.html
  4. // SMART Attributes and Background: http://en.wikipedia.org/wiki/S.M.A.R.T.
  5. // SMART Attributes Overview: http://www.t13.org/Documents/UploadedDocuments/docs2005/e05171r0-ACS-SMARTAttributes_Overview.pdf
  6. namespace SmartDataApp
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Management;
  11. using System.Runtime.InteropServices;
  12. public enum SmartAttributeType : byte
  13. {
  14. ReadErrorRate = 0x01,
  15. ThroughputPerformance = 0x02,
  16. SpinUpTime = 0x03,
  17. StartStopCount = 0x04,
  18. ReallocatedSectorsCount = 0x05,
  19. ReadChannelMargin = 0x06,
  20. SeekErrorRate = 0x07,
  21. SeekTimePerformance = 0x08,
  22. PowerOnHoursPOH = 0x09,
  23. SpinRetryCount = 0x0A,
  24. CalibrationRetryCount = 0x0B,
  25. PowerCycleCount = 0x0C,
  26. SoftReadErrorRate = 0x0D,
  27. SATADownshiftErrorCount = 0xB7,
  28. EndtoEnderror = 0xB8,
  29. HeadStability = 0xB9,
  30. InducedOpVibrationDetection = 0xBA,
  31. ReportedUncorrectableErrors = 0xBB,
  32. CommandTimeout = 0xBC,
  33. HighFlyWrites = 0xBD,
  34. AirflowTemperatureWDC = 0xBE,
  35. TemperatureDifferencefrom100 = 0xBE,
  36. GSenseErrorRate = 0xBF,
  37. PoweroffRetractCount = 0xC0,
  38. LoadCycleCount = 0xC1,
  39. Temperature = 0xC2,
  40. HardwareECCRecovered = 0xC3,
  41. ReallocationEventCount = 0xC4,
  42. CurrentPendingSectorCount = 0xC5,
  43. UncorrectableSectorCount = 0xC6,
  44. UltraDMACRCErrorCount = 0xC7,
  45. MultiZoneErrorRate = 0xC8,
  46. WriteErrorRateFujitsu = 0xC8,
  47. OffTrackSoftReadErrorRate = 0xC9,
  48. DataAddressMarkerrors = 0xCA,
  49. RunOutCancel = 0xCB,
  50. SoftECCCorrection = 0xCC,
  51. ThermalAsperityRateTAR = 0xCD,
  52. FlyingHeight = 0xCE,
  53. SpinHighCurrent = 0xCF,
  54. SpinBuzz = 0xD0,
  55. OfflineSeekPerformance = 0xD1,
  56. VibrationDuringWrite = 0xD3,
  57. ShockDuringWrite = 0xD4,
  58. DiskShift = 0xDC,
  59. GSenseErrorRateAlt = 0xDD,
  60. LoadedHours = 0xDE,
  61. LoadUnloadRetryCount = 0xDF,
  62. LoadFriction = 0xE0,
  63. LoadUnloadCycleCount = 0xE1,
  64. LoadInTime = 0xE2,
  65. TorqueAmplificationCount = 0xE3,
  66. PowerOffRetractCycle = 0xE4,
  67. GMRHeadAmplitude = 0xE6,
  68. DriveTemperature = 0xE7,
  69. HeadFlyingHours = 0xF0,
  70. TransferErrorRateFujitsu = 0xF0,
  71. TotalLBAsWritten = 0xF1,
  72. TotalLBAsRead = 0xF2,
  73. ReadErrorRetryRate = 0xFA,
  74. FreeFallProtection = 0xFE,
  75. }
  76. public class SmartData
  77. {
  78. readonly Dictionary<SmartAttributeType, SmartAttribute> attributes;
  79. readonly ushort structureVersion;
  80. public SmartData(byte[] arrVendorSpecific)
  81. {
  82. attributes = new Dictionary<SmartAttributeType, SmartAttribute>();
  83. for (int offset = 2; offset < arrVendorSpecific.Length; )
  84. {
  85. var a = FromBytes<SmartAttribute>(arrVendorSpecific, ref offset, 12);
  86. // Attribute values 0x00, 0xfe, 0xff are invalid
  87. if (a.AttributeType != 0x00 && (byte)a.AttributeType != 0xfe && (byte)a.AttributeType != 0xff)
  88. {
  89. attributes[a.AttributeType] = a;
  90. }
  91. }
  92. structureVersion = (ushort)(arrVendorSpecific[0] * 256 + arrVendorSpecific[1]);
  93. }
  94. public ushort StructureVersion
  95. {
  96. get
  97. {
  98. return this.structureVersion;
  99. }
  100. }
  101. public SmartAttribute this[SmartAttributeType v]
  102. {
  103. get
  104. {
  105. return this.attributes[v];
  106. }
  107. }
  108. public IEnumerable<SmartAttribute> Attributes
  109. {
  110. get
  111. {
  112. return this.attributes.Values;
  113. }
  114. }
  115. static T FromBytes<T>(byte[] bytearray, ref int offset, int count)
  116. {
  117. IntPtr ptr = IntPtr.Zero;
  118. try
  119. {
  120. ptr = Marshal.AllocHGlobal(count);
  121. Marshal.Copy(bytearray, offset, ptr, count);
  122. offset += count;
  123. return (T)Marshal.PtrToStructure(ptr, typeof(T));
  124. }
  125. finally
  126. {
  127. if (ptr != IntPtr.Zero)
  128. {
  129. Marshal.FreeHGlobal(ptr);
  130. }
  131. }
  132. }
  133. }
  134. [StructLayout(LayoutKind.Sequential)]
  135. public struct SmartAttribute
  136. {
  137. public SmartAttributeType AttributeType;
  138. public ushort Flags;
  139. public byte Value;
  140. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  141. public byte[] VendorData;
  142. public bool Advisory
  143. {
  144. get
  145. {
  146. return (Flags & 0x1) == 0x0; // Bit 0 unset?
  147. }
  148. }
  149. public bool FailureImminent
  150. {
  151. get
  152. {
  153. return (Flags & 0x1) == 0x1; // Bit 0 set?
  154. }
  155. }
  156. public bool OnlineDataCollection
  157. {
  158. get
  159. {
  160. return (Flags & 0x2) == 0x2; // Bit 0 set?
  161. }
  162. }
  163. }
  164. public class Program
  165. {
  166. public static void Main()
  167. {
  168. try
  169. {
  170. var searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData");
  171. foreach (ManagementObject queryObj in searcher.Get())
  172. {
  173. Console.WriteLine("-----------------------------------");
  174. Console.WriteLine("MSStorageDriver_ATAPISmartData instance");
  175. Console.WriteLine("-----------------------------------");
  176. var arrVendorSpecific = (byte[])queryObj.GetPropertyValue("VendorSpecific");
  177. // Create SMART data from 'vendor specific' array
  178. var d = new SmartData(arrVendorSpecific);
  179. foreach (var b in d.Attributes)
  180. {
  181. Console.Write("{0} :{1} : ", b.AttributeType, b.Value);
  182. foreach (byte vendorByte in b.VendorData)
  183. {
  184. Console.Write("{0:x} ", vendorByte);
  185. }
  186. Console.WriteLine();
  187. }
  188. }
  189. }
  190. catch (ManagementException e)
  191. {
  192. Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
  193. }
  194. }
  195. }

字符串
}
最大的问题是弄清楚它的含义,因为它实际上是“特定于供应商”的。数据被组织成12字节的属性数据块。数组的第一个字节给出了属性块的数量。每个属性块的格式为:
项目数据-0和1未知通常为零属性-3状态-4未知通常为零值-6最差-7,8原始值-9,10,11未知通常为零
我在这里找到这些:http://www.i-programmer.info/projects/38-windows/208-disk-drive-dangers.html?start=2

ippsafx7

ippsafx71#

请注意,并非所有SMART HDD(不包括USB和NVME驱动器)信息都可以从单个查询中获得。
您需要查询以下所有内容以确定当前值、最差值、阈值、驱动器状态和属性状态:

  • Win32_Windows驱动器
  • MSSTRR Driver_FailurePredictStatus
  • MSSTRR Driver_FailurePredictData
  • MSSTRR Driver_FailurePredictError保持

有关详细说明所有相关SMART HDD信息的C#/PHP综合解决方案,请参阅此解决方案http://www.know24.net/blog/C+WMI+HDD+SMART+Information.aspx(请注意:我拥有此开发博客)

更新2023-10-24
GitHub:https://github.com/krugertech/Krugertech.IO.Smart
Nuget:https://www.nuget.org/packages/Krugertech.IO.Smart

请随意捐款。

展开查看全部

相关问题