如何确定驱动器是否为外部驱动器

本文关键字:驱动器 外部 是否 何确定 | 更新日期: 2023-09-27 18:29:12

如何确定驱动器是否是通过usb插入的外部驱动器?我检查了DriveInfo.DriveType,但当我的1TB外部驱动器通过usb插入时,它显示为固定驱动器。

想法?

如何确定驱动器是否为外部驱动器

根据Floyd Pink的评论,我使用了这个链接。这使我能够确定设备是否为外部设备。

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_LogicalDiskDriveInfo.DriveType将我的外部类型显示为"本地磁盘",我无法使用它来确定驱动器是否为外部。

您可以使用WMI带有

Select * from Win32_LogicalDisk

http://www.jpsoftwaretech.com/vba/using-wmi-services-in-vba/drive-information-local-network-mapped-drives/

那里有

 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

我在寻找获取外部驱动器列表的方法时遇到了这个问题,我发现通过更新语法可以简化很多。此外,一些USB驱动器是UAS或USB连接的SCSI,这些类型的驱动器的接口实际上显示为SCSI而不是USB。从我的测试中,我发现只检查物理磁盘的介质类型中的"外部"answers"可移动"就足够了。

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;
}