RedKelvin
asked on
Get physical id for flash disk
Hi,
I am trying to get the physical hard disk serial number of my usb flash drive, in C# preferrably, but VB.net would be just as good
Please note I am not trying to get the volume serial number, as this changes with ghosting and formatting. I want the hardware serial.
I have tried the following code, and it works fine for physical drives, but returns nothing for my flash drive
http://www.codeproject.com/csharp/hard_disk_serialNo.asp
I am trying to get the physical hard disk serial number of my usb flash drive, in C# preferrably, but VB.net would be just as good
Please note I am not trying to get the volume serial number, as this changes with ghosting and formatting. I want the hardware serial.
I have tried the following code, and it works fine for physical drives, but returns nothing for my flash drive
http://www.codeproject.com/csharp/hard_disk_serialNo.asp
ASKER
Thanks Bob, but I do not want the volume serial number as this can change, I am after the factory set hardware serial.
I have learnt that not all of these drives have this number set, and this may be the case with my drive
I have learnt that not all of these drives have this number set, and this may be the case with my drive
That is a good point (I missed that from your question). I don't know of any specific way to get the hardware serial from a flash drive. I found some code on the Internet, but it didn't work either.
Bob
Bob
ASKER
I pretty much have it sorted now, basically not all flash drives have a hardware serial number. But for the ones that do you can get the plug and play id using 'msinfo32' from the command line.
Or using VB.Net, you can get it using the following code
Public Function GetHardwareSerialNumber_US B() As ArrayList
Dim ar As New ArrayList
Dim ms As ManagementObjectSearcher
Dim moReturn As ManagementObjectCollection
Dim mo As ManagementObject
ms = New ManagementObjectSearcher(" Select * from Win32_PnPEntity where name = 'USB Mass Storage Device'")
moReturn = ms.Get()
For Each mo In moReturn
ar.Add(mo("DeviceID"))
Next
Return ar
End Function
that will return all the PNP ids for all connected usb drives as an arraylist
Or using VB.Net, you can get it using the following code
Public Function GetHardwareSerialNumber_US
Dim ar As New ArrayList
Dim ms As ManagementObjectSearcher
Dim moReturn As ManagementObjectCollection
Dim mo As ManagementObject
ms = New ManagementObjectSearcher("
moReturn = ms.Get()
For Each mo In moReturn
ar.Add(mo("DeviceID"))
Next
Return ar
End Function
that will return all the PNP ids for all connected usb drives as an arraylist
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//Add a reference to System.Management.dll to the project.
using System;
using System.Management;
public class LogicalDisk
{
public enum DiskType
{
Unknown,
NoRootDirectory,
Removable,
Local,
Network,
CD,
RAM
} //DiskType
public string Name;
public long Freespace;
public long Size;
public DiskType DriveType;
public string VolumeName;
public string VolumeSerialNumber;
public static LogicalDisk[] Disks
{
get
{
string query = "Select * from Win32_LogicalDisk";
// Create a searcher for logical disks.
ManagementObjectSearcher searcher = new ManagementObjectSearcher(q
// Get all the disk entries.
ManagementObjectCollection
// Create an array of LogicalDisk entries.
LogicalDisk[] listDisks = new LogicalDisk[results.Count]
// Transfer the object settings to the array.
int count = 0;
foreach (ManagementObject diskObject in results)
{
LogicalDisk currentDisk = new LogicalDisk();
currentDisk.Name = (string)diskObject["Name"]
currentDisk.Freespace = Convert.ToInt64(diskObject
currentDisk.Size = Convert.ToInt64(diskObject
currentDisk.DriveType = (DiskType)Convert.ToInt32(
currentDisk.VolumeName = (string)diskObject["Volume
currentDisk.VolumeSerialNu
listDisks[count] = currentDisk;
count += 1;
} //for
return listDisks;
} //get
} //Disks
} //class
Look at VolumeSerialNumber.
Bob