How to identify the manufacturer of USB flash drives?

I need my program to work only with some USB flash drives (from one manufacturer) and ignore all other USB flash drives (from other manufacturers).

Is it possible to verify that a specific USB stick is inserted in Windows using .NET 2.0? as?

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

+6
usb
source share
8 answers

EDIT: Added code to print a 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 

Full sample code:

 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 these features should help you distinguish original USB drives from others. Test a few knobs to see if the values ​​match. See the full link for Win32_DiskDrive properties here:

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

Check if this article is right for you:

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

+11
source share

You can use unmanaged Win32 API calls to handle this.

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

+2
source share

Go through Win32 CM_ (Device Management) or WMI and capture the PNP identifier. Look for the VID (vendor identifier).

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

+2
source share

You can get this information through WMI. The following is a vbs script (copy to text file with .vbs to run) that uses WMI to get some information about Win32_DiskDrive objects. Manufacturer information may simply say β€œStandard Disc,” 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 
+2
source share

If the Win32_DiskDrive objects Win32_DiskDrive not provide the information you are looking for, you can also look at the Win32_PhysicalMedia WMI object class. They have Manufacturer, Model, PartNumber, and description properties that you might find useful.

+1
source share
0
source share

Hi try this 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 

A model may be more useful than a Manufacturer. You are viewing FirmwareRevision if you want to block your application for only one manufacturer and one (some) firmware.

Hope this helps.

0
source share

Just in case, if someone else is crazy enough to do this in C ++ - CLI, here is the smink port 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 link to the System.Management library in the properties of my object.

0
source share

All Articles