尝试在我的应用程序运行的任何计算机上从连接的硬盘驱动器中取出一些SMART信息。
我正在将WMI用于该程序中的许多其他内容,而我所看到的关于SMART的每个问题都引用了Win32_DiskDrive。但是,这里的数据非常小,可能不是SMART - 我正在搜索“旋转重试计数”等信息。有任何想法吗?
答案 0 :(得分:8)
您使用的是错误的类(您需要MSStorageDriver_ATAPISmartData)。
要更改所需的属性,请将byte SpinRetryCount = 0x0A;
更改为您希望的任何值(例如,吞吐量性能为0x02)
[StructLayout(LayoutKind.Sequential)]
public struct Attribute
{
public byte AttributeID;
public ushort Flags;
public byte Value;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] VendorData;
}
static void getSMARTAttr()
{
try
{
Attribute AtributeInfo;
ManagementScope Scope = new ManagementScope(String.Format("\\\\{0}\\root\\WMI", "localhost"), null);
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT VendorSpecific FROM MSStorageDriver_ATAPISmartData");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
byte SpinRetryCount = 0x0A;
int Delta = 12;
foreach (ManagementObject WmiObject in Searcher.Get())
{
byte[] VendorSpecific = (byte[])WmiObject["VendorSpecific"];
for (int offset = 2; offset < VendorSpecific.Length; )
{
if (VendorSpecific[offset] == SpinRetryCount)
{
IntPtr buffer = IntPtr.Zero;
try
{
buffer = Marshal.AllocHGlobal(Delta);
Marshal.Copy(VendorSpecific, offset, buffer, Delta);
AtributeInfo = (Attribute)Marshal.PtrToStructure(buffer, typeof(Attribute));
Console.WriteLine("AttributeID {0}", AtributeInfo.AttributeID);
Console.WriteLine("Flags {0}", AtributeInfo.Flags);
Console.WriteLine("Value {0}", AtributeInfo.Value);
//if you want HEX values use this line
//Console.WriteLine("Value {0}", BitConverter.ToString(AtributeInfo.VendorData));
//if you want INT values use this line
Console.WriteLine("Data {0}", BitConverter.ToInt32(AtributeInfo.VendorData, 0));
}
finally
{
if (buffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
}
offset += Delta;
}
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
请记住,如果你得到0以外的任何东西,你需要买一个新的硬盘! 此代码也需要UAC提升,因此您需要以管理员身份运行应用程序,否则您将获得异常。