使用 c# 如何提取有关本地计算机上存在的硬盘驱动器的信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/232979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:11:24  来源:igfitidea点击:

using c# how can I extract information about the hard drives present on the local machine

c#hard-drive

提问by AndyM

I'm looking to get data such as Size/Capacity, Serial No, Model No, Heads Sectors, Manufacturer and possibly SMART data.

我希望获得诸如尺寸/容量、序列号、型号、头部扇区、制造商和可能的 SMART 数据等数据。

采纳答案by Eoin Campbell

You can use WMI Calls to access info about the hard disks.

您可以使用 WMI 调用来访问有关硬盘的信息。

//Requires using System.Management; & System.Management.dll Reference

//需要使用System.Management; & 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");

回答by jmcd

You can use WMIto get most of the information you want, and there's an introduction to WMI here.

您可以使用WMI得到你最想要的信息,而且也介绍了WMI这里

回答by Tamas Czinege

You should use the System.Managementnamespace:

您应该使用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).aspx

http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx

回答by Stu Mackellar

The easiest way is to use WMI to get the required information. Take at look at the documentation for Win32___DiskDrivein MSDN, which contains a variety of standard drive properties. 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. Here's some basic sample code to enumerate all drives and get their properties:

最简单的方法是使用 WMI 来获取所需的信息。查看MSDN 中Win32___DiskDrive的文档,其中包含各种标准驱动器属性。您还可以尝试使用 MSStorageDriver_ATAPISmartData WMI 类,我目前找不到任何文档,但应该包含您正在寻找的所有 SMART 数据。下面是枚举所有驱动器并获取其属性的一些基本示例代码:

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();
}