15

Windows 7 平台,C#

我使用以下语句列出所有驱动器:

DriveInfo[] drives = DriveInfo.GetDrives();

然后我可以使用 DriveType 找出所有这些可移动磁盘:

foreach (var drive in drives)
{
     if(drive.DriveType == DriveType.Removable)
         yield return drive;
}

现在我的问题是,SD卡盘和U盘共享同一个driveType: Removable,那我怎么只能找到U盘呢?

谢谢!

4

2 回答 2

7

您可以ManagementObjectSearcher利用它来查询USB的磁盘驱动器,然后获取相应的单元号并仅返回DriveInfo结果RootDirectory.Name集中包含的单元号。

使用 LINQ 查询表达式:

static IEnumerable<DriveInfo> GetUsbDevices()
{
    IEnumerable<string> usbDrivesLetters = from drive in new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get().Cast<ManagementObject>()
                                           from o in drive.GetRelated("Win32_DiskPartition").Cast<ManagementObject>()
                                           from i in o.GetRelated("Win32_LogicalDisk").Cast<ManagementObject>()
                                           select string.Format("{0}\\", i["Name"]);

    return from drive in DriveInfo.GetDrives()
           where drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name)
           select drive;
}

使用 LINQ 扩展方法:

static IEnumerable<DriveInfo> GetUsbDevices()
{
    IEnumerable<string> usbDrivesLetters = new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get().Cast<ManagementObject>()
        .SelectMany(drive => drive.GetRelated("Win32_DiskPartition").Cast<ManagementObject>())
        .SelectMany(o => o.GetRelated("Win32_LogicalDisk").Cast<ManagementObject>())
        .Select(i => Convert.ToString(i["Name"]) + "\\");

    return DriveInfo.GetDrives().Where(drive => drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name));
}

使用 foreach:

static IEnumerable<string> GetUsbDrivesLetters()
{                
    foreach (ManagementObject drive in new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get())
        foreach (ManagementObject o in drive.GetRelated("Win32_DiskPartition"))
            foreach (ManagementObject i in o.GetRelated("Win32_LogicalDisk"))
                yield return string.Format("{0}\\", i["Name"]);
}

static IEnumerable<DriveInfo> GetUsbDevices()
{
    IEnumerable<string> usbDrivesLetters = GetUsbDrivesLetters();
    foreach (DriveInfo drive in DriveInfo.GetDrives())
        if (drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name))
            yield return drive;
}

要使用ManagementObject,您需要添加对System.Management

我没有很好地测试它,因为我现在没有任何SD卡,但我希望它有所帮助

于 2015-07-22T10:16:55.707 回答
1

我不得不在一个较旧的项目中检查 USB 设备并像这样解决它:

 Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface;
 deviceInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)
 string name = new string(deviceInterface.dbcc_name);
 Guid g = new Guid(deviceInterface.dbcc_classguid);
 if (g.ToString() == "a5dcbf10-6530-11d2-901f-00c04fb951ed")
 {*DO SOMETHING*}

我得到 GUID 并检查设备 GUID 是否是 USB-GUID。

于 2015-07-22T09:36:39.207 回答