|
/// <summary>
/// 获取硬盘信息
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static List<HardDisk> GetDrivers(InterfaceType type)
{
List<HardDisk> disks = new List<HardDisk>();
string strQuery = "select * from Win32_DiskDrive";
if (type == InterfaceType.USB)
strQuery += " Where InterfaceType = 'USB'";
else if (type == InterfaceType.IDE)
strQuery += " Where InterfaceType = 'IDE'";
SelectQuery sq = new SelectQuery(strQuery);
ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
foreach (ManagementObject disk in mos.Get())
{
string DeviceID = disk["DeviceID"].ToString();
foreach (ManagementObject partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + DeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
{
string query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
foreach (ManagementObject disk1 in new ManagementObjectSearcher(query).Get())
{
var diviceID = disk1["DeviceID"].ToString().Trim();
var Name = disk1["Name"].ToString().Trim();
var freeSpace = disk1["FreeSpace"].ToString().Trim();
var size = disk1["Size"].ToString().Trim();
var code = disk["SerialNumber"] == null ? "" : disk["SerialNumber"].ToString().Trim();
disks.Add(new HardDisk
{
Name = Name,
Free = freeSpace,
Size = size,
Code = code,
Type = type,
});
}
}
}
return disks;
}
|
|