简体   繁体   English

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#检索本地机器SID? - How can I retrieve the local Machine SID using C#? 获取有关逻辑驱动器的信息(UWP、C#、Windows) - Get information about logical drives (UWP, C#, Windows) 如何使用C#编辑虚拟硬盘(vhd)? - How to edit Virtual Hard Drives(vhd) with C#? 我可以使用C#将用户信息存储到本地存储中吗 - Can I store user information to local storage using c# 如何使用 C# 运行空间从本地计算机读取 Azure VM 中存在的远程 yaml 文件 - How to read remote yaml file present in Azure VM from local machine using C# Runspace 如何使用 C# 在 VM 中获取本地机器名称? - How can i Get local Machine Name in VM using C#? 如何使用c#以编程方式将证书安装到本地计算机存储中? - How can I install a certificate into the local machine store programmatically using c#? 如何在Winrt App中通过C#在本地驱动器中创建文件夹 - How to create a folder in local drives through c# in winrt app ¿如何使用 C# 获取网络有效载荷中的信息? - ¿How can i get information in Network Payload using C#? 如何以编程方式获取有关其他解决方案的信息或进行操作? (Visual Studio,C#) - How can I programmatically get information about or manipulate another solution? (Visual Studio, C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM