How do I get the internal serial number of a USB-Stick or USB-HardDrive in C#?
+17
A:
Try this:
//import the System.Management namespace at the top in your "using" statement. Then in a method, or on a button click:
ManagementObjectSearch theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}
Yuval A
2009-01-16 10:32:11
Thanks, I've been looking for something like this forever!
Abdullah Jibaly
2009-01-30 04:45:19
A:
Hola esto no me funciono:
//import the System.Management namespace at the top in your "using" statement. Then in a method, or on a button click:ManagementObjectSearch theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");foreach (ManagementObject currentObject in theSearcher.Get()){ ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'"); MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());}
esto si funciona:
Public Function GetSerialNumber(ByVal DriveLetter As String) As String
Dim wmi_ld, wmi_dp, wmi_dd As ManagementObject
Dim temp, parts(), ans As String
ans = ""
' get the Logical Disk for that drive letter
wmi_ld = New ManagementObject("Win32_LogicalDisk.DeviceID='" & _
DriveLetter.TrimEnd("\"c) & "'")
' get the associated DiskPartition
For Each wmi_dp In wmi_ld.GetRelated("Win32_DiskPartition")
' get the associated DiskDrive
For Each wmi_dd In wmi_dp.GetRelated("Win32_DiskDrive")
' There is a bug in WinVista that corrupts some of the fields
' of the Win32_DiskDrive class if you instantiate the class via
' its primary key (as in the example above) and the device is
' a USB disk. Oh well... so we have go thru this extra step
Dim wmi As New ManagementClass("Win32_DiskDrive")
' loop thru all of the instances. This is silly, we shouldn't
' have to loop thru them all, when we know which one we want.
For Each obj As ManagementObject In wmi.GetInstances
' do the DeviceID fields match?
If obj("DeviceID").ToString = wmi_dd("DeviceID").ToString Then
' the serial number is embedded in the PnPDeviceID
temp = obj("PnPDeviceID").ToString
If Not temp.StartsWith("USBSTOR") Then
Throw New ApplicationException(DriveLetter & " doesn't appear to be USB Device")
End If
parts = temp.Split("\&".ToCharArray)
' The serial number should be the next to the last element
ans = parts(parts.Length - 2)
End If
Next
Next
Next
Return ans
End Function
Juan Camilo
2010-09-30 14:00:58
@balexandre: The only part of his explanation that is not in English is the first sentence of the answer and code. All of the source is in English with English comments. It seems to be in VB.Net rather than C#, though it's all Framework calls so it should be pretty easy to translate it to C#. All that being said, Yuval A's WMI answer seems much more readable than this one.
Brian
2010-09-30 14:12:52
@Brian, I speak and write 7 different languages, it's not about that, it's a matter of be polite with others and write in the same language as everyone else.
balexandre
2010-09-30 17:02:08