如何确定驱动器是外部驱动器 [英] How to determine if drive is external drive
问题描述
我如何能够确定一个驱动器通过USB插入一个外部驱动器?我查了 DriveInfo.DriveType 但我通过USB插入1TB外置硬盘它显示为固定驱动器。
How can I determine if a drive is a external drive plugged in through usb ? I checked the DriveInfo.DriveType but with my 1TB external drive plugged in through usb it shows up as a fixed drive.
思考?
推荐答案
根据从弗洛伊德粉红我用这个<一评论href="http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/a3d4dbc7-c63a-46d5-a191-c73a4fca233a/"相对=nofollow>链接。这使我能够确定设备是否是外部的或没有。
Based on a comment from Floyd Pink I used this link. This allows me to determine whether a device is external or not.
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;
}
使用WMI SELECT * FROM的Win32_LogicalDisk
在Royi南月明的回答和 DriveInfo.DriveType
显示我的外部类型本地磁盘的,我不能用它来确定驱动器是否是外部的。
Using WMI Select * from Win32_LogicalDisk
as in Royi Namir's answer and DriveInfo.DriveType
show my external type as 'Local Disk' which I can't use to determine whether the drive is external.
这篇关于如何确定驱动器是外部驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!