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

7

我需要让我的程序仅与某一制造商的USB闪存驱动器配合使用,并忽略所有其他制造商的USB闪存驱动器。

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

如果我通过WMI找到它,是否可以确定USB驱动器所在的驱动器号?

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驱动器和其他驱动器。测试几个U盘以检查数值是否相同。这里有完整的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


2

2

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

我在Win32_USBControllerDeviceWin32_DiskDrive下看到刚插入的设备的信息。


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

1
如果 Win32_DiskDrive 对象无法提供您所需的信息,您还可以查看 WMI 对象的 Win32_PhysicalMedia 类。它们具有制造商、型号、零件编号和描述属性,可能会证明有用。

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 库的引用。


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。

希望对您有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接