如何确定驱动器是否是通过USB插入的外置驱动器?我检查了DriveInfo.DriveType,但是我的1TB外置驱动器通过USB接入,它显示为固定驱动器。
想法?
答案 0 :(得分:4)
您可以使用WMI 与
Select * from Win32_LogicalDisk
你有
Select Case .DriveType
Case 0
strDriveType = "Unknown"
Case 1
strDriveType = "No Root Directory"
Case 2
strDriveType = "Removable Disk"
Case 3
strDriveType = "Local Disk"
Case 4
strDriveType = "Network Drive"
Case 5
strDriveType = "Compact Disc"
Case 6
strDriveType = "RAM Disk"
End Select
答案 1 :(得分:4)
根据Floyd Pink的评论,我使用了link。这允许我确定设备是否是外部设备。
public bool IsProjectOnExternalDisk(string driveLetter)
{
bool retVal = false;
driveLetter = driveLetter.TrimEnd('\\');
// browse all USB WMI physical disks
foreach (ManagementObject drive in new ManagementObjectSearcher("select DeviceID, MediaType,InterfaceType from Win32_DiskDrive").Get())
{
// associate physical disks with partitions
ManagementObjectCollection partitionCollection = new ManagementObjectSearcher(String.Format("associators of {{Win32_DiskDrive.DeviceID='{0}'}} " + "where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).Get();
foreach (ManagementObject partition in partitionCollection)
{
if (partition != null)
{
// associate partitions with logical disks (drive letter volumes)
ManagementObjectCollection logicalCollection = new ManagementObjectSearcher(String.Format("associators of {{Win32_DiskPartition.DeviceID='{0}'}} " + "where AssocClass= Win32_LogicalDiskToPartition", partition["DeviceID"])).Get();
foreach (ManagementObject logical in logicalCollection)
{
if (logical != null)
{
// finally find the logical disk entry
ManagementObjectCollection.ManagementObjectEnumerator volumeEnumerator = new ManagementObjectSearcher(String.Format("select DeviceID from Win32_LogicalDisk " + "where Name='{0}'", logical["Name"])).Get().GetEnumerator();
volumeEnumerator.MoveNext();
ManagementObject volume = (ManagementObject)volumeEnumerator.Current;
if (driveLetter.ToLowerInvariant().Equals(volume["DeviceID"].ToString().ToLowerInvariant()) &&
(drive["MediaType"].ToString().ToLowerInvariant().Contains("external") || drive["InterfaceType"].ToString().ToLowerInvariant().Contains("usb")))
{
retVal = true;
break;
}
}
}
}
}
}
return retVal;
}
在Royi Namir的答案中使用WMI Select * from Win32_LogicalDisk
,DriveInfo.DriveType
将我的外部类型显示为'本地磁盘',我无法用它来确定驱动器是否为外部
答案 2 :(得分:0)
我遇到这个问题,寻找一种获取外部驱动器列表的方法,我发现通过更新语法可以简化很多工作。另外,某些USB驱动器是UAS或USB连接的SCSI,这些类型的驱动器的接口实际上将显示为SCSI,而不是USB。通过测试,我发现只需检查物理磁盘介质类型中的“外部”和“可移动”即可。
public List<DriveInfo> getExternalDrives()
{
var drives = DriveInfo.GetDrives();
var externalDrives = new List<DriveInfo>();
var allPhysicalDisks = new ManagementObjectSearcher("select MediaType, DeviceID from Win32_DiskDrive").Get();
foreach (var physicalDisk in allPhysicalDisks)
{
var allPartitionsOnPhysicalDisk = new ManagementObjectSearcher($"associators of {{Win32_DiskDrive.DeviceID='{physicalDisk["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition").Get();
foreach(var partition in allPartitionsOnPhysicalDisk)
{
if (partition == null)
continue;
var allLogicalDisksOnPartition = new ManagementObjectSearcher($"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition").Get();
foreach(var logicalDisk in allLogicalDisksOnPartition)
{
if (logicalDisk == null)
continue;
var drive = drives.Where(x => x.Name.StartsWith(logicalDisk["Name"] as string, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
var mediaType = (physicalDisk["MediaType"] as string).ToLowerInvariant();
if (mediaType.Contains("external") || mediaType.Contains("removable"))
externalDrives.Add(drive);
}
}
}
return externalDrives;
}