locked
DriveInfo.GetDrives().DriveType returns fixed drive for external USB drive RRS feed

  • Question

  • I have a need to detect external USB Drive and some reason drivetype return external USB Storage device (USB Harddrive) as fixed device (same as installed harddrive in the computer).

    Anyone has a way to find out how to detect whether external USB Harddrive is attached or not.

    Thanks in advance.

    Wednesday, July 16, 2014 2:36 PM

Answers

  • Hi Hemen,

    > Anyone has a way to find out how to detect whether external USB Harddrive is attached or not.

    I assume you want to determine one hard drive when necessary. I think code maybe like the following.

    static void Main(string[] args)
    
            {
    
                var alldrives = System.IO.DriveInfo.GetDrives();
    
                foreach (System.IO.DriveInfo di in alldrives)
    
                {
    
                    Console.WriteLine(string.Format("{0} drive {1} externaldisk", di.Name, IsProjectOnExternalDisk(di.Name) ? "is" : "is not"));
    
                }
    
                Console.Read();
    
            }
    
    public static 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;
    
            }
    

    If I misunderstand something, could you please post more information to make thing clearly?

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, July 18, 2014 6:49 AM

All replies

  • Hi Hemen,

    You can use Win32_LogicalDisk WMI class to get hard drive information. http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx.

    See code sample from the following link. http://stackoverflow.com/questions/9891854/how-to-determine-if-drive-is-external-drive.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Thursday, July 17, 2014 2:49 AM
  • Those URLs were not helpful to me.
    Thursday, July 17, 2014 6:46 PM
  • Hi Hemen,

    > Anyone has a way to find out how to detect whether external USB Harddrive is attached or not.

    I assume you want to determine one hard drive when necessary. I think code maybe like the following.

    static void Main(string[] args)
    
            {
    
                var alldrives = System.IO.DriveInfo.GetDrives();
    
                foreach (System.IO.DriveInfo di in alldrives)
    
                {
    
                    Console.WriteLine(string.Format("{0} drive {1} externaldisk", di.Name, IsProjectOnExternalDisk(di.Name) ? "is" : "is not"));
    
                }
    
                Console.Read();
    
            }
    
    public static 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;
    
            }
    

    If I misunderstand something, could you please post more information to make thing clearly?

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, July 18, 2014 6:49 AM