C # sample code to validate a USB flash drive serial number
Private list<string> _serialnumber = new list<string> (); public void Matchdriveletterwithseral () {_serialnumber.clear (); 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 ())); } } } }