C#如何知道移动硬盘是USB驱动器或SD卡? [英] C# how to know if removable disk is a usb drive, or a sd card?
问题描述
在Windows 7平台,C#
Windows 7 platform, C#
我用下面的语句来列出所有驱动器:
I use the following statement to list all drives:
DriveInfo[] drives = DriveInfo.GetDrives();
然后我可以使用的DriveType找出所有的可移动磁盘:
then I can use DriveType to find out all those removable disks:
foreach (var drive in drives)
{
if(drive.DriveType == DriveType.Removable)
yield return drive;
}
现在我的问题是,SD卡盘和USB闪存盘共享相同DRIVETYPE:可拆卸的,所以,我怎么只能找到USB闪存盘了?
now my problem is, SD-card disk and USB flashdisk shared same driveType: Removable, so how can i only find USB flashdisk out?
谢谢!
推荐答案
您可以用它来查询是USB磁盘驱动器利用 ManagementObjectSearcher $ C $的c>,然后获得相应的单元号和只返回
DriveInfo
,其中 RootDirectory.Name
包含在结果集。
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.
使用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
To use ManagementObject
you need to add reference to System.Management
我没有测试它好,因为现在我不知道有SD卡,但我希望它能帮助
I haven't tested it well because now I don't have any SD card, but I hope it helps
这篇关于C#如何知道移动硬盘是USB驱动器或SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!