7

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

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

如果我通过 WMI 找到它,我能以某种方式确定 USB 驱动器在哪个驱动器号上吗?

4

8 回答 8

12

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


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

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

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

于 2008-09-23T21:53:27.767 回答
2

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

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

于 2008-09-23T21:50:53.390 回答
2

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

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

于 2008-09-23T21:54:30.387 回答
2

您可以通过 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
于 2008-09-23T21:55:25.797 回答
1

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

于 2008-09-23T22:15:46.403 回答
0

也许#usblib:

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

于 2008-09-23T21:43:08.370 回答
0

嗨,在使用 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。

希望能帮助到你。

于 2008-09-23T21:58:49.893 回答
0

以防万一其他人足够疯狂地在 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注意:我必须在我的项目属性中手动添加对库的引用。

于 2011-08-04T09:40:23.063 回答