C#獲得U盤序列號的辦法。本站提示廣大學習愛好者:(C#獲得U盤序列號的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#獲得U盤序列號的辦法正文
本文實例講述了C#獲得U盤序列號的辦法。分享給年夜家供年夜家參考。詳細以下:
using System.Management; private List<string> _serialNumber = new List<string>(); /// <summary> /// 挪用這個函數將本機一切U盤序列號存儲到_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; }
挪用辦法:
matchDriveLetterWithSerial(); string[] aa = _serialNumber.ToArray(); for (int i = 0; i < aa.Length; i++) { aa[i].ToString(); //這裡便可以拿湧現在一切的U盤序列號 }
願望本文所述對年夜家的C#法式設計有所贊助。