How can I get VID/PID from USB Flash drives in C#?

user1172635 picture user1172635 · Apr 7, 2012 · Viewed 7.1k times · Source

I need to perform a check on all drives and see if any of the VIDs/PID match a specific one, if it does I need to get the drive letter of that flash drive. Thanks to all!

Answer

John Bartels picture John Bartels · Apr 7, 2012

WMI should be able to handle this...

You will have to add a Reference to the System.Management dll and you will need to have the: "using System.Management;" line... See Link At Bottom for Screenshots, more thorough explanation...

using System.Management;
// Get all the disk drives

ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

// Loop through each object (disk) retrieved by WMI

foreach (ManagementObject moDisk in mosDisks.Get())

{

    // Add the HDD to the list (use the Model field as the item's caption)

    cmbHdd.Items.Add(moDisk["Model"].ToString());

}


private void cmbHdd_SelectedIndexChanged(object sender, EventArgs e)

{

// Get all the disk drives from WMI that match the Model name selected in the ComboBox

ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");

// Loop through the drives retrieved, although it should normally be only one loop going on here

foreach (ManagementObject moDisk in mosDisks.Get())

{

    // Set all the fields to the appropriate values

    lblType.Text = "Type: " + moDisk["MediaType"].ToString();

    lblModel.Text = "Model: " + moDisk["Model"].ToString();

    lblSerial.Text = "Serial: " + moDisk["SerialNumber"].ToString();

    lblInterface.Text = "Interface: " + moDisk["InterfaceType"].ToString();

    // The capacity in gigabytes is easily calculated 

    lblCapacity.Text = "Capacity: " + moDisk["Size"].ToString() + " bytes (" + Math.Round(((((double)Convert.ToDouble(moDisk["Size"]) / 1024) / 1024) / 1024), 2) + " GB)";

    lblPartitions.Text = "Partitions: " + moDisk["Partitions"].ToString();

    lblSignature.Text = "Signature: " + moDisk["Signature"].ToString();

    lblFirmware.Text = "Firmware: " + moDisk["FirmwareRevision"].ToString();

    lblCylinders.Text = "Cylinders: " + moDisk["TotalCylinders"].ToString();

    lblSectors.Text = "Sectors: " + moDisk["TotalSectors"].ToString();

    lblHeads.Text = "Heads: " + moDisk["TotalHeads"].ToString();

    lblTracks.Text = "Tracks: " + moDisk["TotalTracks"].ToString();

    lblBytesPerSect.Text = "Bytes per Sector: " + moDisk["BytesPerSector"].ToString();

    lblSectorsPerTrack.Text = "Sectors per Track: " + moDisk["SectorsPerTrack"].ToString();

    lblTracksPerCyl.Text = "Tracks per Cylinder: " + moDisk["TracksPerCylinder"].ToString();

    }

}

From MSDN the win32 Class for CIM_DiskDrive has the following parameters:

*It looks as if "DeviceID" is what you want...

class Win32_DiskDrive : CIM_DiskDrive
{
  uint16   Availability;
  uint32   BytesPerSector;
  uint16   Capabilities[];
  string   CapabilityDescriptions[];
  string   Caption;
  string   CompressionMethod;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  uint64   DefaultBlockSize;
  string   Description;
  string   DeviceID;
  boolean  ErrorCleared;
  string   ErrorDescription;
  string   ErrorMethodology;
  string   FirmwareRevision;
  uint32   Index;
  datetime InstallDate;
  string   InterfaceType;
  uint32   LastErrorCode;
  string   Manufacturer;
  uint64   MaxBlockSize;
  uint64   MaxMediaSize;
  boolean  MediaLoaded;
  string   MediaType;
  uint64   MinBlockSize;
  string   Model;
  string   Name;
  boolean  NeedsCleaning;
  uint32   NumberOfMediaSupported;
  uint32   Partitions;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  uint32   SCSIBus;
  uint16   SCSILogicalUnit;
  uint16   SCSIPort;
  uint16   SCSITargetId;
  uint32   SectorsPerTrack;
  string   SerialNumber;
  uint32   Signature;
  uint64   Size;
  string   Status;
  uint16   StatusInfo;
  string   SystemCreationClassName;
  string   SystemName;
  uint64   TotalCylinders;
  uint32   TotalHeads;
  uint64   TotalSectors;
  uint64   TotalTracks;
  uint32   TracksPerCylinder;
};

Top Portion oF Code Taken From:

http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html