聪明。硬盘数据在C# [英] S.M.A.R.T. Hard Drive Data in C#

查看:260
本文介绍了聪明。硬盘数据在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想拉断从连接硬盘我的应用程序可以运行在任何一台电脑上的一些SMART信息。

Just trying to pull off some SMART info from connected Hard Drives on any computer my application will run on.

我使用WMI对于很多其他的东西在节目中,以及有关SMART的每一个问题我已经看了提到Win32_DiskDrive。然而,在这里的数据是真的很最小的,可能不是聪明 - 我在寻找的信息,如旋转重试计数。有任何想法吗?

I'm using WMI for a lot of other stuff in the program, and every question about SMART I've looked at makes reference to Win32_DiskDrive. However, the data in here is really quite minimal and probably not SMART - I'm searching for information such as 'Spin Retry Count'. Any ideas?

推荐答案

您使用了错误的类(您想MSStorageDriver_ATAPISmartData)。
要改变什么属性,你想改变字节SpinRetryCount = 0x0A的; 你希望的任何值(例如0X02的吞吐量性能)

You are using the wrong class (you want MSStorageDriver_ATAPISmartData). To change what attribute you want change byte SpinRetryCount = 0x0A; to whatever value you wish (e.g. 0x02 for throughput performance)

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

And remember that if you get anything other than 0, you need to buy a new hard drive! Also this code requires UAC elevation, so you need to run the application as an administrator or you will get an exception.

这篇关于聪明。硬盘数据在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆