[英]How to lookup Hard Drive model with C#?
在我正在編寫的程序的一部分中,我正在嘗試提取有關指定本地硬盤驅動器的設備信息。 我已經能夠使用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:\\"));
我現在遇到的問題是DriveInfo類似乎沒有Model屬性。 我看了一遍,但無法找到一種方法來構造一個返回值的方法,該方法將返回驅動器的模型,就像在設備管理器中可以看到的那樣。
任何幫助將不勝感激,謝謝!
遺憾的是,您無法使用DriveInfo
類獲取Drive的制造商和型號。
你必須回到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"]);
}
不確定是否還需要考慮已安裝的驅動器:
foreach(ManagementObject volume in new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
...
}
我不完全確定你是否可以在不使用低級api的情況下獲得該信息。 這篇文章應該可以幫助您實現目標。
http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I
鏈接的快速摘要:
System.Management
庫的引用 然后你可以使用:
var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject disk in disks.Get())
{
Console.WriteLine(disk["Model"].ToString());
Console.WriteLine("\tSerial: " + disk["SerialNumber"]);
}
您需要使用較低級別的API來獲取此信息,即使這樣,它仍然可能不准確。* Win32 API中公開了硬盤驅動器的內部詳細信息,您仍然可以通過WMI訪問C# 。
*:請注意,這仍然僅限於Windows能夠看到的硬件信息。 在某些情況下,它不會或不能准確(例如,使用RAID陣列,Windows將N個驅動器視為單個驅動器)。
這里有些東西也適合您,隨時隨地調整它
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.