如何使用 WMI 获取 USB 设备的驱动器号 [英] How to get the drive letter of USB device using WMI

查看:33
本文介绍了如何使用 WMI 获取 USB 设备的驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要跟踪 C# 应用程序中的 USB 插入和移除事件,因此我根据 SO 上的其他问题提出了以下想法.

I need to track the USB insertion and removal events from the C# application so I came up with the following ideas based on the other questions on SO.

我不能用这个方法

var drives = DriveInfo.GetDrives()
    .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable).ToList();

因为当您需要区分连接的设备和断开的设备时,这会导致很多麻烦(新设备可以具有与之前缓存的设备相同的驱动器号和名称).

because it can lead to a lot of troubles when you need to differentiate between connected and disconnected devices (the new ones can have the same drive letter and name like the ones that was previously cached).

于是我决定使用WMI来解决这个问题,但是我发现通过Win32_USBHub类无法获取指定USB设备的盘符.然后我想我可以执行另一个这样的查询

So I decided to use WMI to solve this problem but I found that it's impossible to get the drive letter of the specified USB device via Win32_USBHub class. Then I thought that I can execute another query like this

foreach (ManagementObject device in new ManagementObjectSearcher(@"SELECT * FROM Win32_USBHub").Get())
{
    string deviceID = (string)device.GetPropertyValue("DeviceID");

    Console.WriteLine("{0}", deviceID);

    string query = string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID='{0}'", deviceID);
    foreach (ManagementObject o in new ManagementObjectSearcher(query).Get())
    {
        string name = (string)o.GetPropertyValue("Name");
        Console.WriteLine("{0}", name);
    }

    Console.WriteLine("==================================");
}

但它根本不起作用 - 每次尝试执行与 Win32_LogicalDisk 表一起使用的查询时,我都会收到异常无效查询".

but it doesn't work at all -- I get an exception "Invalid query" every time when I try to execute query that works with the Win32_LogicalDisk table.

为什么?我究竟做错了什么?我该如何解决?也许有更好的方法来解决这个问题?

Why? What am I doing wrong? How can I fix it? Maybe there is a better ways to solve this problem?

提前致谢.

推荐答案

您收到一个异常,因为您的 deviceID 包含需要转义的字符(反斜杠).使用简单的替换,您不应该得到异常.

You get an exception because your deviceID contains characters that need to be escaped (backslashes). With simple replace you shouldn't get the exception.

string query = string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID='{0}'", deviceID.Replace(@"\", @"\\"));

但是,从 WMI 获取 USB 驱动器盘符要复杂一些.您需要学习一些课程,如@MSalters 在评论中发布的链接所述:

However, getting USB drive letter from WMI is a little more complicated. You need to go through a few classes, as stated in a link that @MSalters posted in his comment:

Win32_DiskDrive-> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition -> Win32_LogicalDiskToPartition -> Win32_LogicalDisk.

此处为我工作:

foreach (ManagementObject device in new ManagementObjectSearcher(@"SELECT * FROM Win32_DiskDrive WHERE InterfaceType LIKE 'USB%'").Get())
{
    Console.WriteLine((string)device.GetPropertyValue("DeviceID"));
    Console.WriteLine((string)device.GetPropertyValue("PNPDeviceID"));                

    foreach (ManagementObject partition in new ManagementObjectSearcher(
        "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + device.Properties["DeviceID"].Value
        + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
    {
        foreach (ManagementObject disk in new ManagementObjectSearcher(
                    "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
                        + partition["DeviceID"]
                        + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
        {
            Console.WriteLine("Drive letter " + disk["Name"]);
        }
    }
}

这篇关于如何使用 WMI 获取 USB 设备的驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆