【问题标题】:C# how to know if removable disk is a usb drive, or a sd card?C#如何知道可移动磁盘是USB驱动器还是SD卡?
【发布时间】:2015-07-22 09:27:00
【问题描述】:

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盘呢?

谢谢!

【问题讨论】:

    标签: c# removable


    【解决方案1】:

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

    使用 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卡,但我希望它可以帮助

    【讨论】:

    • 嗨 @codroipo,usbDrivesLetters 返回所有可移动驱动器,包括 sd 卡。但我喜欢 ManagementObjectSearcher,它看起来更专业,哈哈,所以请你多帮点如何只过滤 USB 驱动器?
    • @OhMyDog 在设备管理器中,您的 SD 卡读卡器在哪个节点下显示?
    • @OhMyDog 你能告诉我new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get().Cast&lt;ManagementObject&gt;().Select(f =&gt; (string)f["PNPDeviceID"]).ToList()的输出是什么吗?
    • 问题已解决。我检查了 pnpDeviceID 发现 USB 闪存和 SD 卡之间的区别,谢谢 codroipo!
    • 在您现在知道该怎么做之后,能否请您修改您的答案以使其正确?谢谢
    【解决方案2】:

    我不得不在一个旧项目中检查 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。

    【讨论】:

    • 我不完全确定,但我认为 OP 没有 GUID
    • 也感谢 npit,但我专注于 codroipo 的回答,抱歉没有尝试你的回答。
    猜你喜欢
    • 2010-12-31
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2014-10-27
    • 2015-12-24
    • 1970-01-01
    • 2011-05-22
    相关资源
    最近更新 更多