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卡盘和U盘共享同一个driveType: Removable,那我怎么只能找到U盘呢?
谢谢!
我不得不在一个旧项目中检查 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。
你可以利用ManagementObjectSearcher
使用它来查询是USB的磁盘驱动器,然后获取相应的单位字母和return只有DriveInfo
其中[=15] =] 包含在结果集中。
使用 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 卡,但我希望它对您有所帮助
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盘呢?
谢谢!
我不得不在一个旧项目中检查 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。
你可以利用ManagementObjectSearcher
使用它来查询是USB的磁盘驱动器,然后获取相应的单位字母和return只有DriveInfo
其中[=15] =] 包含在结果集中。
使用 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 卡,但我希望它对您有所帮助