Click here to Skip to main content
15,888,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the serial number, model,... of the usb key connected on to pc?

Something like this:
C#
ManagementObjectSearcher USB = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
          ManagementObjectCollection USBinfo = USB.Get();
          string info = "";
          foreach (ManagementObject MO in USBinfo)
          {
              info = (string)MO["SerialNumber"];
          }


But this gives me the serial number for disk, not usb key.
Posted
Comments
Member 4347041 31-Aug-13 3:52am    
Also tried this:

ManagementObjectSearcher MOS = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
ManagementObjectCollection model = MOS.Get();
string mark = "";

foreach (ManagementObject obj in model)
{
if (obj["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE") & obj["InterfaceType"].ToString() == "USB")
{
mark = (string)(obj["PNPDeviceID"].ToString());

}

}

textBox7.Text = "ID is: " + mark.ToString();

and i don't get anything.
Member 13443380 31-May-22 17:28pm    
Hello

try changing this code ::
"if (obj["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE") & obj["InterfaceType"].ToString() == "USB")"
to this one ::
"if (obj["Name"].ToString().Contains(("\\\\.\\PHYSICALDRIVE")) & obj["InterfaceType"].ToString() == "USB")"

this must show the exact usb serial number !

and also the casting method here -> "mark = (string)(obj["PNPDeviceID"].ToString());" is redundant ! you may better remove that cast (string).

 
Share this answer
 
Comments
Joezer BH 1-Sep-13 6:52am    
5ed!
Sergey Alexandrovich Kryukov 1-Sep-13 10:52am    
Thank you.
—SA
Try querying for the WMI class Win32_LogicalDisk, it has one property called VolumeSerialNumber. Other properties like DriveType and MediaType will help you identifying the type or drive. With the property PNPDeviceID it could be possible to track it down to the physical USB device information.
There's a long disccussion here http://social.technet.microsoft.com/Forums/windowsserver/en-US/09c9814a-38fa-4b16-bc8f-01329882a791/powershell-wmi-get-usb-storage-devices-only[^] with hints that could help.
 
Share this answer
 
Comments
Member 4347041 2-Sep-13 5:32am    
The only thing working is the "PNPDeviceID" the DriveType and VolumeSerialNumber show error "Not found".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900