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

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

如果我通过WMI找到它,我可以以某种方式确定USB驱动器所在的驱动器号吗?

有帮助吗?

解决方案

编辑:添加了打印硬盘信件的代码。


检查此示例是否适合您。它使用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/ EN-US /库/ aa394132(VS.85)的.aspx

检查这篇文章是否对您有任何帮助:

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

其他提示

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

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

通过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

如果 Win32_DiskDrive 对象没有产生您要查找的信息,您还可以查看 Win32_PhysicalMedia WMI对象类。它们具有可能有用的制造商,型号,零件编号和描述属性。

您可以尝试使用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。

希望它有所帮助。

以防万一其他人在C ++中做到这一点疯狂 - CLI,这里是smink回答的一个端口:

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

注意:我必须在我的项目属性中手动添加对 System.Management 库的引用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top