5

只是试图从我的应用程序将运行的任何计算机上连接的硬盘驱动器中提取一些 SMART 信息。

我将 WMI 用于程序中的许多其他内容,并且我查看过的有关 SMART 的每个问题都参考了 Win32_DiskDrive。但是,这里的数据非常少,可能不是 SMART - 我正在搜索诸如“Spin Retry Count”之类的信息。有任何想法吗?

4

1 回答 1

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

于 2012-03-26T19:09:28.840 回答