[英]How to lookup Hard Drive model with C#?
In part of a program I am writing, I'm trying to pull device information about specified local hard drives. 在我正在编写的程序的一部分中,我正在尝试提取有关指定本地硬盘驱动器的设备信息。 I've been able to create a few value returning methods using the DriveInfo class like this: 我已经能够使用DriveInfo类创建一些值返回方法,如下所示:
//Gets drive format
public string GetDriveFormat(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
{
return drive.DriveFormat;
}
}
return "";
}
//Example of use
MessageBox.Show(GetDriveFormat("C:\\"));
The problem I'm running into now is that there doesn't seem to be a Model property to the DriveInfo class. 我现在遇到的问题是DriveInfo类似乎没有Model属性。 I've looked all over but am unable to find a way to construct a value returning method that will return the model of a drive like what is viewable in device manager. 我看了一遍,但无法找到一种方法来构造一个返回值的方法,该方法将返回驱动器的模型,就像在设备管理器中可以看到的那样。
Any help would be greatly appreciated, Thanks! 任何帮助将不胜感激,谢谢!
Unfortunately, you cannot get the Drive's Manufacturer and Model using the DriveInfo
class. 遗憾的是,您无法使用DriveInfo
类获取Drive的制造商和型号。
You'll have to resort back to WMI: 你必须回到WMI:
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM Win32_DiskDrive");
ManagementObjectSearcher res = new ManagementObjectSearcher(q);
foreach (ManagementObject o in res.Get()) {
Console.WriteLine("Caption = " + o["Caption"]);
Console.WriteLine("DeviceID = " + o["DeviceID"]);
Console.WriteLine("Decsription = " + o["Description"]);
Console.WriteLine("Manufacturer = " + o["Manufacturer"]);
Console.WriteLine("MediaType = " + o["MediaType"]);
Console.WriteLine("Model = " + o["Model"]);
Console.WriteLine("Name = " + o["Name"]);
// only in Vista, 2008 & etc: //Console.WriteLine("SerialNumber = " + o["SerialNumber"]);
}
Not sure if you need to consider mounted drives as well: 不确定是否还需要考虑已安装的驱动器:
foreach(ManagementObject volume in new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
...
}
I'm not entirely sure if you can get that info without using lower level api's. 我不完全确定你是否可以在不使用低级api的情况下获得该信息。 This post should help you achieve your goal. 这篇文章应该可以帮助您实现目标。
http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I
Quick summary of the link: 链接的快速摘要:
System.Management
library 添加对System.Management
库的引用 Then you can use: 然后你可以使用:
var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject disk in disks.Get())
{
Console.WriteLine(disk["Model"].ToString());
Console.WriteLine("\tSerial: " + disk["SerialNumber"]);
}
You'll need to use a lower-level API to get this information, and even then it still might not be accurate.* The internal details of the hard drives is exposed in the Win32 APIs, which you can still access in C# through WMI. 您需要使用较低级别的API来获取此信息,即使这样,它仍然可能不准确。* Win32 API中公开了硬盘驱动器的内部详细信息,您仍然可以通过WMI访问C# 。
*: Note that this is still limited to the hardware information as Windows is able to see it. *:请注意,这仍然仅限于Windows能够看到的硬件信息。 In some conditions, it won't or can't be accurate (eg with a RAID array, where Windows sees N drives as a single drive). 在某些情况下,它不会或不能准确(例如,使用RAID阵列,Windows将N个驱动器视为单个驱动器)。
here is something that can also work for you feel free to tweak it as you please 这里有些东西也适合您,随时随地调整它
String drive = "c";
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
disk.Get();
Console.WriteLine(disk["VolumeName"]);
foreach (var props in disk.Properties)
{
Console.WriteLine(props.Name + " " + props.Value);
}
Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.