Home > DeveloperSection > Forums > Get Drive Letter and Name using C#
Dag Hammarskjold
Dag Hammarskjold

Total Post:24

Points:168
Posted on    October-16-2013 7:03 AM

 C# C# 
Ratings:


 1 Reply(s)
 3805  View(s)
Rate this:

I am suffering with a program that is how can I get hard disks/ usb's, but it only return me the drive letter not the name.

DriveInfo[] driveInfo = DriveInfo.GetDrives();
Console.WriteLine("Detected Drives: ");
for(int i = 0; i < driveInfo.Count(); i++)
{
     Console.WriteLine("Drive " + i + ": " + driveInfo[i].Name);
}
return driveInfo;
and this prints:
Drive 0: C:\
Drive 1: E:\

but I want the name such as

Drive 0: C:\ Local disk
Drive 1: E:\ SacnDisk USB

How can I perform that task please help me!



lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    October-16-2013 7:57 AM

Try this

 try
        {
            DriveInfo[] driveInfo = DriveInfo.GetDrives();
            foreach (DriveInfo drive in driveInfo)
            {
                Console.WriteLine("Drive:" + drive.Name);
                Console.WriteLine("Drive Type:" + drive.DriveType);
                if (drive.IsReady == true)
                {
                    Console.WriteLine("Vol Label:" + drive.VolumeLabel);
                    Console.WriteLine("File System: " + drive.DriveFormat);
                }
            }
        }
        catch (Exception)
        {
            throw;
        }

Don't want to miss updates? Please click the below button!

Follow MindStick