[英]using c# how can I extract information about the hard drives present on the local machine
我希望获得诸如尺寸/容量、序列号、型号、头部扇区、制造商和可能的 SMART 数据等数据。
You can use WMI Calls to access info about the hard disks.您可以使用 WMI 调用来访问有关硬盘的信息。
//Requires using System.Management; //需要使用System.Management; & System.Management.dll Reference
& System.Management.dll 参考
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "bytes");
You should use the System.Management namespace:您应该使用System.Management命名空间:
System.Management.ManagementObjectSearcher ms =
new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject mo in ms.Get())
{
System.Console.Write(mo["Model");
}
For details on the members of the Win32_DiskDrive class, check out:有关 Win32_DiskDrive 类成员的详细信息,请查看:
http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspxhttp://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx
The easiest way is to use WMI to get the required information.最简单的方法是使用 WMI 来获取所需的信息。 Take at look at the documentation for Win32___DiskDrive in MSDN, which contains a variety of standard drive properties.
查看 MSDN 中Win32___DiskDrive的文档,其中包含各种标准驱动器属性。 You can also try using the MSStorageDriver_ATAPISmartData WMI class, which I can't find any docs for at the moment, but should have all of the SMART data that you're looking for.
您还可以尝试使用 MSStorageDriver_ATAPISmartData WMI 类,我目前找不到任何文档,但应该包含您正在寻找的所有 SMART 数据。 Here's some basic sample code to enumerate all drives and get their properties:
下面是枚举所有驱动器并获取其属性的一些基本示例代码:
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drive in drives)
{
foreach (PropertyData property in drive.Properties)
{
Console.WriteLine("Property: {0}, Value: {1}", property.Name, property.Value);
}
Console.WriteLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.