Sumrak
Sumrak

Reputation: 3748

how to determine USB Flash drive manufacturer?

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?

Upvotes: 7

Views: 11575

Answers (8)

Jon Cage
Jon Cage

Reputation: 37500

Just in case anyone else is crazy enough to do this in C++-CLI, here's a port of smink's answer:

using namespace System;
using namespace System::Management;

void GetUSBDeviceList()
{
    try
    {
        ManagementObjectSearcher^ searcher =
            gcnew ManagementObjectSearcher("root\\CIMV2",
            "SELECT * FROM Win32_DiskDrive");

        for each (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"]);
            for each (ManagementObject^ b in queryObj->GetRelated("Win32_DiskPartition"))
            {
                Console::WriteLine("  Name: {0}", b["Name"]);
                for each (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();
}

Note: I had to manually add a reference to the System.Management library in my porject properties.

Upvotes: 0

Jorge Ferreira
Jorge Ferreira

Reputation: 97899

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

Upvotes: 12

Curtis
Curtis

Reputation: 1189

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.

Upvotes: 1

Donald
Donald

Reputation: 481

Hi try this in using WMI

Option Explicit
Dim objWMIService, objItem, colItems, strComputer

' On Error Resume Next
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select Manufacturer from Win32_DiskDrive")

For Each objItem in colItems
Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
   "Manufacturer: " & objItem.Manufacturer & VbCr & _
   "Model: " & objItem.Model
Next

Modelcould be more helpful than Manufacturer. You look at FirmwareRevision if you want to lock you app now to only one Manufacturer and one (some) Firmware Revision.

Hope it helps.

Upvotes: 0

Curtis
Curtis

Reputation: 1189

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

Upvotes: 2

Kris Kumler
Kris Kumler

Reputation: 6307

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.

Upvotes: 2

sallen
sallen

Reputation: 466

You could use unmanaged Win32 API calls to handle this.

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

Upvotes: 2

fryguybob
fryguybob

Reputation: 4410

Perhaps #usblib:

http://www.icsharpcode.net/OpenSource/SharpUSBLib/

Upvotes: 0

Related Questions