Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if drive is external drive

Tags:

c#

.net

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.

Thoughts ?

like image 299
Web Avatar asked Mar 27 '12 14:03

Web


People also ask

How do you tell if a drive is internal or external?

If you haven't already noticed, their names give away their location which is their main difference. An internal hard drive is built within the computer. An external hard drive is not. You need to connect it to the computer using a cable to have access to it.

How do I know if my hard drive is SSD or external?

Simply press the Windows key + R keyboard shortcut to open the Run box, type dfrgui and press Enter. When the Disk Defragmenter window is shown, look for the Media type column and you can find out which drive is solid state drive (SSD), and which one is hard disk drive (HDD).

How do I know my drive type?

Open the System Information file. In the System Information window, click the + symbol next to Components. Click the + next to Storage and click Drives. In the right-side of the window, you see information about the hard drive including its capacity and serial number.

Is HDD internal or external?

Internal hard drives are located inside your computer. Unlike an external hard drive, these drives are not portable and only be used by the computer they inhabit. There are two main kinds of internal hard drives: the HDD (hard disk drive) and the SSD (solid state drive). HDDs are the more traditional kind.


1 Answers

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.

like image 55
Web Avatar answered Sep 20 '22 22:09

Web