Given the letter of a drive, how can I determine what type of drive it is?
For example, whether E:\ is a USB drive, a network drive or a local hard drive.
Have a look at DriveInfo
's DriveType
property.
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (var drive in drives)
{
string driveName = drive.Name; // C:\, E:\, etc:\
System.IO.DriveType driveType = drive.DriveType;
switch (driveType)
{
case System.IO.DriveType.CDRom:
break;
case System.IO.DriveType.Fixed:
// Local Drive
break;
case System.IO.DriveType.Network:
// Mapped Drive
break;
case System.IO.DriveType.NoRootDirectory:
break;
case System.IO.DriveType.Ram:
break;
case System.IO.DriveType.Removable:
// Usually a USB Drive
break;
case System.IO.DriveType.Unknown:
break;
}
}