如何确定USB闪存驱动器制造商?

时间:2020-03-06 14:37:43  来源:igfitidea点击:

我需要我的程序只能与某些USB闪存驱动器(来自单个制造商)一起使用,而忽略所有其他USB闪存驱动器(来自任何其他制造商)。

是否可以使用.NET 2.0检查Windows上是否插入了特定的USB卡?如何?

如果通过WMI找到它,是否可以确定USB驱动器在哪个驱动器盘符上?

解决方案

也许#usblib:

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

我们可以使用非托管Win32 API调用来处理此问题。

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

编辑:添加代码以打印驱动器号。

检查此示例是否适合我们。它使用WMI。

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

完整的代码示例:

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();
        }
    }
}

我认为这些属性应该可以将正版USB驱动器与其他驱动器区分开。用几个笔式驱动器进行测试,以检查值是否相同。请在此处查看Win32_DiskDrive属性的完整参考:

http://msdn.microsoft.com/zh-CN/library/aa394132(VS.85).aspx

检查本文是否对我们也有帮助:

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

通过Win32 CM_(设备管理)或者WMI并获取PNP ID。查找VID(供应商ID)。

我在" Win32_USBControllerDevice"和" Win32_DiskDrive"下看到了刚刚插入的设备的信息。

我们也许可以通过WMI获得此信息。下面是一个vbs脚本(复制到运行.vbs的文本文件),该脚本使用WMI获取有关Win32_DiskDrive对象的一些信息。
制造商信息可能只是说"标准磁盘驱动器",但"型号"可能符合要求。

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

嗨,尝试使用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

模型可能比制造商更有帮助。如果要立即将应用程序锁定为仅一个制造商和一个(某些)固件修订版,则可以查看FirmwareRevision。

希望能帮助到你。

如果" Win32_DiskDrive"对象没有产生我们要查找的信息,则我们还可以查看WMI对象的Win32_PhysicalMedia类。它们具有制造商,型号,零件号和描述属性,这些属性可能被证明是有用的。