代碼從網上搜來,由於運行不成功,經自己修改過。
private List<string> _serialNumber = new List<string>();
/// <summary>
/// 調用這個函數將本機所有隨身碟序號儲存到_serialNumber中
/// </summary>
private void matchDriveLetterWithSerial()
{
string[] diskArray;
string driveNumber;
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
foreach (ManagementObject dm in searcher.Get())
{
getValueInQuotes(dm["Dependent"].ToString());
diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');
driveNumber = diskArray[0].Remove(0, 6).Trim();
var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject disk in disks.Get())
{
if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB")
{
_serialNumber.Add(parseSerialFromDeviceID(disk["PNPDeviceID"].ToString()));
}
}
}
}
private static string parseSerialFromDeviceID(string deviceId)
{
var splitDeviceId = deviceId.Split('\\');
var arrayLen = splitDeviceId.Length - 1;
var serialArray = splitDeviceId[arrayLen].Split('&');
var serial = serialArray[0];
return serial;
}
private static string getValueInQuotes(string inValue)
{
var posFoundStart = inValue.IndexOf("\"");
var posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);
var parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);
return parsedValue;
}