簡體   English   中英

C#如何知道可移動磁盤是USB驅動器還是SD卡?

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

Windows 7平台,C#

我使用以下語句列出所有驅動器:

DriveInfo[] drives = DriveInfo.GetDrives();

然后我可以使用DriveType找出所有可移動磁盤:

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

現在我的問題是,SD卡磁盤和USB閃存盤共享相同的driveType:可移動,那么我怎么才能找到USB閃存盤?

謝謝!

您可以利用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卡,但我希望它有所幫助

我不得不在一個較舊的項目中檢查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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM