how to determine USB Flash drive manufacturer?

10,577

Solution 1

EDIT: Added code to print drive letter.


Check if this example works for you. It uses WMI.

Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
...
Console.WriteLine("    Name: {0}", c["Name"]); // here it will print drive letter

The full code sample:

namespace WMISample
{
    using System;
    using System.Management;

    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_DiskDrive");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
                    Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
                    Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                    Console.WriteLine("Model: {0}", queryObj["Model"]);
                    foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
                    {
                        Console.WriteLine("  Name: {0}", b["Name"]);
                        foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                        {
                            Console.WriteLine("    Name: {0}", c["Name"]); // here it will print drive letter
                        }
                    }
                    // ...
                    Console.WriteLine("--------------------------------------------");
                }      
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e.StackTrace);
            }

            Console.ReadLine();
        }
    }
}

I think those properties should help you distinguish genuine USB drives from the others. Test with several pen drives to check if the values are the same. See full reference for Win32_DiskDrive properties here:

http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx

Check if this article is also of any help to you:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/48a9758c-d4db-4144-bad1-e87f2e9fc979

Solution 2

Going through either Win32 CM_ (Device Management) or WMI and grabbing the PNP ID. Look for VID (Vendor ID).

I see information for the device I just inserted under Win32_USBControllerDevice and Win32_DiskDrive.

Solution 3

You may be able to get this information through WMI. Below is a vbs script (copy to text file with .vbs to run) which uses WMI to get some information about Win32_DiskDrive objects. The Manufacturer info might just say Standard Disk Drive, but the Model number may have what you are looking for.

Set Drives = GetObject("winmgmts:{impersonationLevel=impersonate,(Backup)}").ExecQuery("select * from Win32_DiskDrive")
for each drive in drives
Wscript.echo "Drive Information:" & vbnewline & _
       "Availability: " & drive.Availability & vbnewline & _
       "BytesPerSector: " & drive.BytesPerSector & vbnewline & _
       "Caption: " & drive.Caption & vbnewline & _
       "CompressionMethod: " & drive.CompressionMethod & vbnewline & _
       "ConfigManagerErrorCode: " & drive.ConfigManagerErrorCode & vbnewline & _
       "ConfigManagerUserConfig: " & drive.ConfigManagerUserConfig & vbnewline & _
       "CreationClassName: " & drive.CreationClassName & vbnewline & _
       "DefaultBlockSize: " & drive.DefaultBlockSize & vbnewline & _
       "Description: " & drive.Description & vbnewline & _
       "DeviceID: " & drive.DeviceID & vbnewline & _
       "ErrorCleared: " & drive.ErrorCleared & vbnewline & _
       "ErrorDescription: " & drive.ErrorDescription & vbnewline & _
       "ErrorMethodology: " & drive.ErrorMethodology & vbnewline & _
       "Index: " & drive.Index & vbnewline & _
       "InterfaceType: " & drive.InterfaceType & vbnewline & _
       "LastErrorCode: " & drive.LastErrorCode & vbnewline & _
       "Manufacturer: " & drive.Manufacturer & vbnewline & _
       "MaxBlockSize: " & drive.MaxBlockSize & vbnewline & _
       "MaxMediaSize: " & drive.MaxMediaSize & vbnewline & _
       "MediaLoaded: " & drive.MediaLoaded & vbnewline & _
       "MediaType: " & drive.MediaType & vbnewline & _
       "MinBlockSize: " & drive.MinBlockSize & vbnewline & _
       "Model: " & drive.Model & vbnewline & _
       "Name: " & drive.Name & vbnewline & _
       "NeedsCleaning: " & drive.NeedsCleaning & vbnewline & _
       "NumberOfMediaSupported: " & drive.NumberOfMediaSupported & vbnewline & _
       "Partitions: " & drive.Partitions & vbnewline & _
       "PNPDeviceID: " & drive.PNPDeviceID & vbnewline & _
       "PowerManagementSupported: " & drive.PowerManagementSupported & vbnewline & _
       "SCSIBus: " & drive.SCSIBus & vbnewline & _
       "SCSILogicalUnit: " & drive.SCSILogicalUnit & vbnewline & _
       "SCSIPort: " & drive.SCSIPort & vbnewline & _
       "SCSITargetId: " & drive.SCSITargetId & vbnewline & _
       "SectorsPerTrack: " & drive.SectorsPerTrack & vbnewline & _
       "Signature: " & drive.Signature & vbnewline & _
       "Size: " & drive.Size & vbnewline & _
       "Status: " & drive.Status & vbnewline & _
       "StatusInfo: " & drive.StatusInfo & vbnewline & _
       "SystemCreationClassName: " & drive.SystemCreationClassName & vbnewline & _
       "SystemName: " & drive.SystemName & vbnewline & _         
       "TotalCylinders: " & drive.TotalCylinders & vbnewline & _         
       "TotalHeads: " & drive.TotalHeads & vbnewline & _        
       "TotalSectors: " & drive.TotalSectors & vbnewline & _        
       "TotalTracks: " & drive.TotalTracks & vbnewline & _         
       "TracksPerCylinder: " & drive.TracksPerCylinder & vbnewline
next

Solution 4

You could use unmanaged Win32 API calls to handle this.

http://www.codeproject.com/KB/system/EnumDeviceProperties.aspx

Solution 5

If Win32_DiskDrive objects do not yield the information you are looking for, you could also look at Win32_PhysicalMedia class of WMI objects. They have Manufacturer, Model, PartNumber, and description properties which may prove useful.

Share:
10,577
Sumrak
Author by

Sumrak

Developing in .NET

Updated on June 21, 2022

Comments

  • Sumrak
    Sumrak almost 2 years

    I need my program to work only with certain USB Flash drives (from a single manufacturer) and ignore all other USB Flash drives (from any other manufacturers).

    is it possible to check that specific USB card is inserted on windows using .NET 2.0? how?

    if I find it through WMI, can I somehow determine which drive letter the USB drive is on?