2012-02-19 94 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提升,因此您需要以管理員身份運行應用程序,否則您將得到一個異常。