c# - 使用 S.M.A.R.T 和 WMI 加载/卸载循环计数

标签 c# windows-7 wmi disk-smart

在尝试开发一个硬盘分析工具时,我试图从我的硬盘的 S.M.A.R.T 数据中获取加载/卸载循环计数的值,我想知道是否有人知道如何做到这一点。 我正在尝试:

  1. 我正在搜索 WMI MSStorageDriver_ATAPISmartData 类数据,其中属性号为 193是我需要的(表示加载/卸载循环计数的属性)
  2. 我得到的数据看起来像

enter image description here

我想我已经接近了,红色的数据与我运行 Everest Home 版本时显示的相同,理想情况下我想要最后一部分(称为数据的属性)

enter image description here

收集此数据的方法:

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); }
}

附言此方法适用于收集 HDD 的温度(这就是 Console.WriteLine("HDD TEMP: "+ arrVendorSpecific[tempIndex + 5].ToString()); 行的全部内容,但我不是确定为什么它的 tempIndex+5

最佳答案

您使用的代码不正确,因为您正在使用安全搜索 (Array.IndexOf) 来查找 S.M.A.R.T Attribute ID(您可能有误报,因为该值可以与数组中的另一个),这些属性的 ID 在文档结构中有一个固定位置(SMART Attribute Overview)。

SMART 属性表

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

属性表中的条目

enter image description here

从这里您可以编写代码来搜索每个属性的位置并获取您要查找的值

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();
        }
    }
}

关于c# - 使用 S.M.A.R.T 和 WMI 加载/卸载循环计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9852979/

相关文章:

c++ - (WMI) ExecMethod 输出参数 - 无论调用结果如何,ResultingSnapshot 都是 NULL,为什么?

c# - 将文件(字节数组)的字符串表示形式转换回 C# 中的文件

c# - 方法重载 C#

c# - 向 ASP.NET MVC4 项目添加类

c# - 从 DbProviderFactories.GetFactory() 获取 Sqlite

windows-7 - 从 Windows 7 升级到 Windows 8 后无法启动 Visual Studio 2012

c++ - 在 Qt 中清除标志/提示

windows-7 - 以编程方式强制图标在 Windows 7 中的 "notification area"/"systray") 中可见

.net - 如何在远程计算机上执行批处理文件?

delphi - 数组的 WMI 值