C# 使用S.M.A.R.T和WMI加载/卸载循环计数
为了开发硬盘分析工具,我试图从硬盘的s.m.a.R.T数据中获取加载/卸载周期计数的值,我想知道是否有人知道如何做到这一点。 我正在尝试的是:C# 使用S.M.A.R.T和WMI加载/卸载循环计数,c#,windows-7,wmi,disk-smart,C#,Windows 7,Wmi,Disk Smart,为了开发硬盘分析工具,我试图从硬盘的s.m.a.R.T数据中获取加载/卸载周期计数的值,我想知道是否有人知道如何做到这一点。 我正在尝试的是: 我正在搜索WMIMSStorage Driver\u ATAPISmartData类数据,其中属性号是我需要的(表示加载/卸载周期计数的属性) 我得到的数据看起来像 我想我很接近了,红色的数据与我运行珠穆朗玛峰家庭版时显示的数据相同,理想情况下,我希望最后一部分是(称为数据的属性) 收集此数据的方法: static void doStuff() {
MSStorage Driver\u ATAPISmartData
类数据,其中属性号是我需要的(表示加载/卸载周期计数的属性)static void doStuff()
{
try
{
byte TEMPERATURE_ATTRIBUTE = 193;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData");
//loop through all the hard disks
foreach (ManagementObject queryObj in searcher.Get())
{
byte[] arrVendorSpecific = (byte[])queryObj.GetPropertyValue("VendorSpecific");
int tempIndex = Array.IndexOf(arrVendorSpecific, TEMPERATURE_ATTRIBUTE);
Console.WriteLine("HDD TEMP: " + arrVendorSpecific[tempIndex + 5].ToString());
foreach (byte dat in arrVendorSpecific)
{
Console.Write(dat.ToString() + " ");
}
}
}
catch (Exception err) { Console.WriteLine(err.Message); }
}
另外,此方法可用于采集硬盘的温度(这就是
控制台.WriteLine(“硬盘温度:+arrVendorSpecific[tempIndex+5].ToString());
行的全部内容,但我不确定为什么它的tempIndex+5您使用的代码不正确,因为您使用的是安全搜索(Array.IndexOf)要查找S.M.A.R.T属性ID
(可能存在误报,因为该值可能与数组中的另一个值匹配),这些属性的ID在文档结构中有一个固定位置()
智能属性表
Offset Length Description
(bytes)
0 2 SMART structure version (this is vendor-specific)
2 12 Attribute entry 1
2+(12) 12 Attribute entry 2
. . .
2+(12*29) 12 Attribute entry 30
属性表中的条目
Offset Length Description
(bytes)
0 2 SMART structure version (this is vendor-specific)
2 12 Attribute entry 1
2+(12) 12 Attribute entry 2
. . .
2+(12*29) 12 Attribute entry 30
从这里,您可以编写一个代码来搜索每个属性的位置,并获得要查找的值
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Runtime.InteropServices;
namespace GetWMI_Info
{
class Program
{
[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 Main(string[] args)
{
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 LoadCycleCount = 0xC1;
int Delta = 12;
foreach (ManagementObject WmiObject in Searcher.Get())
{
byte[] VendorSpecific = (byte[])WmiObject["VendorSpecific"];
for (int offset = 2; offset < VendorSpecific.Length; )
{
if (VendorSpecific[offset] == LoadCycleCount)
{
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);
Console.WriteLine("Value {0}", BitConverter.ToString(AtributeInfo.VendorData));
}
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用制度管理;
使用系统文本;
使用System.Runtime.InteropServices;
命名空间GetWMI\u信息
{
班级计划
{
[StructLayout(LayoutKind.Sequential)]
公共结构属性
{
公共字节属性ID;
美国国旗;
公共字节值;
[Marshallas(UnmanagedType.ByValArray,SizeConst=8)]
公共字节[]供应商数据;
}
静态void Main(字符串[]参数)
{
尝试
{
属性信息;
ManagementScope=new ManagementScope(String.Format(“\\{0}\\root\\WMI”,“localhost”),null);
Scope.Connect();
ObjectQuery查询=新的ObjectQuery(“从MSStorage Driver_ATAPISmartData中选择Vendor Specific”);
ManagementObjectSearcher search=新的ManagementObjectSearcher(范围、查询);
字节LoadCycleCount=0xC1;
int Delta=12;
foreach(Searcher.Get()中的ManagementObject WMIOObject)
{
字节[]供应商专用=(字节[])WmiObject[“供应商专用”];
对于(int offset=2;offset