How to determine if drive is external drive

后端 未结 3 797
清歌不尽
清歌不尽 2020-12-17 02:05

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

相关标签:
3条回答
  • 2020-12-17 02:21

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

    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.

    0 讨论(0)
  • 2020-12-17 02:27

    I came across this question looking for a way to get a list of external drives, and I found that I could simplify a lot by updating the syntax. Also, some USB drives are UAS or USB attached SCSI, and the interface for those types of drives will actually show up as SCSI and not USB. From my tests, I've found that just checking for the terms "external" and "removable" in the physical disk's media type is sufficient.

    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;
    }
    
    0 讨论(0)
  • 2020-12-17 02:29

    you can use WMI with

    Select * from Win32_LogicalDisk
    

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

    there you have

     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
    
    0 讨论(0)
提交回复
热议问题