CodeI searched from the Internet and modified it because the operation was unsuccessful.
Private List < String > _ Serialnumber = New List < String > ();
/// <Summary>
/// Call this function to store the serial numbers of all USB flash drives on the machine to _ 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;
}