简体   繁体   English

C#如何知道可移动磁盘是USB驱动器还是SD卡?

[英]C# how to know if removable disk is a usb drive, or a sd card?

Windows 7 platform, C# Windows 7平台,C#

I use the following statement to list all drives: 我使用以下语句列出所有驱动器:

DriveInfo[] drives = DriveInfo.GetDrives();

then I can use DriveType to find out all those removable disks: 然后我可以使用DriveType找出所有可移动磁盘:

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

now my problem is, SD-card disk and USB flashdisk shared same driveType: Removable, so how can i only find USB flashdisk out? 现在我的问题是,SD卡磁盘和USB闪存盘共享相同的driveType:可移动,那么我怎么才能找到USB闪存盘?

thanks! 谢谢!

You can take advantage of ManagementObjectSearcher using it to query the disk drives that are USB, then obtain the corresponding unit letter and return only the DriveInfo of which RootDirectory.Name is contained in the result set. 您可以利用ManagementObjectSearcher使用它来查询USB磁盘驱动器,然后获取相应的单位字母并仅返回结果集中包含RootDirectory.NameDriveInfo

Using LINQ Query Expressions: 使用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;
}

Using LINQ Extension Methods: 使用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));
}

Using foreach: 使用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;
}

To use ManagementObject you need to add reference to System.Management 要使用ManagementObject您需要添加对System.Management引用

I haven't tested it well because now I don't have any SD card, but I hope it helps 我没有测试好,因为现在我没有任何SD卡,但我希望它有所帮助

I had to check for USB-Devices in an older project and solved it like this: 我不得不在一个较旧的项目中检查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*}

I get the GUID and check if the devices GUID is the USB-GUID. 我得到GUID并检查设备GUID是否是USB-GUID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM