2012-02-19 90 views
4

试图在我的应用程序运行的任何计算机上从连接的硬盘驱动器中取出一些SMART信息。S.M.A.R.T. C#中的硬盘驱动器数据#

我在程序中使用了很多其他东西的WMI,每个关于SMART的问题都引用了Win32_DiskDrive。但是,这里的数据真的非常少,可能不是SMART--我正在寻找诸如“Spin Retry Count”之类的信息。有任何想法吗?

+0

下面是答案:http://blogs.msdn.com/b/clemensv/archive/2011/04/11/reading-atapi-smart-data-from-drives-using-net-temperature-anyone。 aspx – Kamil 2012-02-19 18:47:29

+0

你在错误的类中搜索。找到MSStorageDriver_ATAPISmartData类并从中读取。 Google MSStorageDriver_ATAPISmartData获取更多信息。 – Kamil 2012-02-19 20:31:23

+0

@CJxD您需要访问MSStorageDriver_ATAPISmartData类,选择它的deta,将正确的字节映射到正确的结构(字节,ushorts,整数等) – 2012-03-26 19:17:43

回答

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提升,因此您需要以管理员身份运行应用程序,否则您将得到一个异常。