我知道一些C + +和Java,但我想自己学习C#。 混乱,我试图读取我的硬盘驱动器的SMART数据。 我有这个C#代码,但我不知道如何修改它来读取额外的内存值:它显然读取“值”值,而不是“最差”或“阈值”值。 我想将这两个数据值(最差和阈值)添加到程序中。 如何做到这一点将有助于我学习一点C#。
C#示例:(我想要使用的)
// (c) Microsoft Corporation // Author: Clemens Vasters ([email protected]) // Code subject to MS-PL: http://opensource.org/licenses/ms-pl.html // SMART Attributes and Background: http://en.wikipedia.org/wiki/SMART // SMART Attributes Overview: http://www.t13.org/Documents/UploadedDocuments/docs2005/e05171r0-ACS-SMARTAttributes_Overview.pdf namespace SmartDataApp { using System; using System.Collections.Generic; using System.Management; using System.Runtime.InteropServices; public enum SmartAttributeType : byte { ReadErrorRate = 0x01,ThroughputPerformance = 0x02,SpinUpTime = 0x03,StartStopCount = 0x04,ReallocatedSectorsCount = 0x05,ReadChannelMargin = 0x06,SeekErrorRate = 0x07,SeekTimePerformance = 0x08,PowerOnHoursPOH = 0x09,SpinRetryCount = 0x0A,CalibrationRetryCount = 0x0B,PowerCycleCount = 0x0C,SoftReadErrorRate = 0x0D,SATADownshiftErrorCount = 0xB7,EndtoEnderror = 0xB8,HeadStability = 0xB9,InducedOpVibrationDetection = 0xBA,ReportedUncorrectableErrors = 0xBB,CommandTimeout = 0xBC,HighFlyWrites = 0xBD,AirflowTemperatureWDC = 0xBE,TemperatureDifferencefrom100 = 0xBE,GSenseErrorRate = 0xBF,PoweroffRetractCount = 0xC0,LoadCycleCount = 0xC1,Temperature = 0xC2,HardwareECCRecovered = 0xC3,ReallocationEventCount = 0xC4,CurrentPendingSectorCount = 0xC5,UncorrectableSectorCount = 0xC6,UlTradMACRCErrorCount = 0xC7,MultiZoneErrorRate = 0xC8,WriteErrorRateFujitsu = 0xC8,OffTrackSoftReadErrorRate = 0xC9,DataAddressMarkerrors = 0xCA,RunOutCancel = 0xCB,SoftECCCorrection = 0xCC,ThermalAsperityRateTAR = 0xCD,FlyingHeight = 0xCE,SpinHighCurrent = 0xCF,SpinBuzz = 0xD0,OfflineseekPerformance = 0xD1,VibrationDuringWrite = 0xD3,ShockDuringWrite = 0xD4,diskShift = 0xDC,GSenseErrorRatealt = 0xDD,LoadedHours = 0xDE,LoadUnloadRetryCount = 0xDF,LoadFriction = 0xE0,LoadUnloadCycleCount = 0xE1,LoadInTime = 0xE2,TorqueAmplificationCount = 0xE3,PowerOffRetractCycle = 0xE4,GMRHeadamplitude = 0xE6,DriveTemperature = 0xE7,HeadFlyingHours = 0xF0,TransferErrorRateFujitsu = 0xF0,TotalLBAsWritten = 0xF1,TotalLBAsRead = 0xF2,ReadErrorRetryRate = 0xFA,FreeFallProtection = 0xFE,} public class SmartData { readonly Dictionary<SmartAttributeType,SmartAttribute> attributes; readonly ushort structureversion; public SmartData(byte[] arrvendorSpecific) { attributes = new Dictionary<SmartAttributeType,SmartAttribute>(); for (int offset = 2; offset < arrvendorSpecific.Length; ) { var a = FromBytes<SmartAttribute>(arrvendorSpecific,ref offset,12); // Attribute values 0x00,0xfe,0xff are invalid if (a.AttributeType != 0x00 && (byte)a.AttributeType != 0xfe && (byte)a.AttributeType != 0xff) { attributes[a.AttributeType] = a; } } structureversion = (ushort)(arrvendorSpecific[0] * 256 + arrvendorSpecific[1]); } public ushort Structureversion { get { return this.structureversion; } } public SmartAttribute this[SmartAttributeType v] { get { return this.attributes[v]; } } public IEnumerable<SmartAttribute> Attributes { get { return this.attributes.Values; } } static T FromBytes<T>(byte[] bytearray,ref int offset,int count) { IntPtr ptr = IntPtr.Zero; try { ptr = Marshal.AllocHGlobal(count); Marshal.copy(bytearray,offset,ptr,count); offset += count; return (T)Marshal.PtrToStructure(ptr,typeof(T)); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } } } [StructLayout(LayoutKind.Sequential)] public struct SmartAttribute { public SmartAttributeType AttributeType; public ushort Flags; public byte Value; [MarshalAs(UnmanagedType.ByValArray,SizeConst = 8)] public byte[] vendorData; public bool Advisory { get { return (Flags & 0x1) == 0x0; // Bit 0 unset? } } public bool FailureImminent { get { return (Flags & 0x1) == 0x1; // Bit 0 set? } } public bool OnlineDataCollection { get { return (Flags & 0x2) == 0x2; // Bit 0 set? } } } public class Program { public static void Main() { try { var searcher = new ManagementObjectSearcher("root\WMI","SELECT * FROM MsstorageDriver_ATAPISmartData"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("MsstorageDriver_ATAPISmartData instance"); Console.WriteLine("-----------------------------------"); var arrvendorSpecific = (byte[])queryObj.GetPropertyValue("vendorSpecific"); // Create SMART data from 'vendor specific' array var d = new SmartData(arrvendorSpecific); foreach (var b in d.Attributes) { Console.Write("{0} :{1} : ",b.AttributeType,b.Value); foreach (byte vendorByte in b.vendorData) { Console.Write("{0:x} ",vendorByte); } Console.WriteLine(); } } } catch (ManagementException e) { Console.WriteLine("An error occurred while querying for WMI data: " + e.Message); } } }
}
最大的问题是弄清楚它是什么意思,因为它确实是“供应商特定的”。 数据被组织成12字节的属性数据块。 数组的第一个字节给出了属性块的数量。 每个属性块的格式为:
.Net和Hadoop – 知道/学习什么和可用的?
应用程序重启API不重新启动失败的应用程序
CLR如何实现COM实现的二进制互操作级别? 还是不?
在繁忙的环境中可靠的文件保存(File.Replace)
当源和目标位于同一个远程文件服务器上时,是否远程处理File.copy?
项目数据-0和1未知通常为零-2属性-3状态-4未知通常为零-5值-6最差-7,8原始值-9,10,11未知通常为零
我在这里发现这些: http : //www.i-programmer.info/projects/38-windows/208-disk-drive-dangers.html?start=2
如何使用2个第三方.net库时,他们的非托pipe依赖冲突?
如何根据托pipe代码在桌面和移动应用程序之间交换数据
如何获得button上的popupbutton,点击uwp中的另一个popupbutton
获取.net应用程序的活动窗口
请注意,并非所有SMART HDD信息都可以从单个WMI查询中获取。
您需要查询以下所有内容以确定当前值,最差值,阈值,驱动器状态和属性状态:
Win32_diskDrive MsstorageDriver_FailurePredictStatus MsstorageDriver_FailurePredictData MsstorageDriver_FailurePredictThresholds
有关C#/ WMI全面解决方案的详细信息,请参阅此解决方案http://www.kNow24.net/blog/C+WMI+HDD+SMART+@R_230_404[email protected] (请注意:我拥有此开发博客)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。