Geekpedia Programming Tutorials






Retrieve a list of disk drives using C#

This code retrieves a list of drives, stores it into an array of strings, and then loops through it to display the drives.

On Thursday, October 18th 2007 at 08:09 PM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 3 votes)
Contextual Ads
More C# Resources
Advertisement
  1. // Store the list of drives into an array of string
  2. string[] DriveList = Environment.GetLogicalDrives();
  3. // Loop through the array
  4. for (int i = 0; i < DriveList.Length; i++)
  5. {
  6.     // Show each drive
  7.     MessageBox.Show(DriveList[i]);
  8. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by tony on Monday, November 24th 2008 at 01:02 AM

Excellent

by TaToosh on Friday, November 28th 2008 at 03:11 PM

how to get only local hard drives, no cd/dvd or mapped network ones?

by Reelix on Thursday, July 30th 2009 at 02:52 AM

static void Main(string[] args)
{

Console.Title = "Storm Disk Space Mailer.";
// Store the list of drives into an array of string
string[] DriveList = Environment.GetLogicalDrives();
// Loop through the array
for (int i = 0; i < DriveList.Length; i )
{
// Show each drive
getSize(DriveList[i].Replace(":\\", ""));
}
Console.WriteLine("Listing Complete - Press enter to close.");
Console.ReadLine();
}

public static void getSize(string drive)
{
ManagementObject diskSize = new ManagementObject("win32_logicaldisk.deviceid=\"" drive ":\"");

diskSize.Get();
double GB = Convert.ToDouble(diskSize["FreeSpace"]);
GB = ((((GB / 1024) / 1024) / 1024));
string GBS = GB.ToString();
try
{
GBS = GBS.Substring(0, (GBS.IndexOf(".") 3));
}
catch
{
// Do Nothing
}
string DriveType = diskSize["DriveType"].ToString();
switch (DriveType)
{
case "0":
DriveType = "Unknown";
break;
case "1":
DriveType = "No Root Directory";
break;
case "2":
DriveType = "Removable Disk";
break;
case "3":
DriveType = "Local Disk";
break;
case "4":
DriveType = "Network Drive";
break;
case "5":
DriveType = "Compact Disc";
break;
case "6":
DriveType = "RAM Disk";
break;
default:
DriveType = "Unknown";
break;
}
if (DriveType == "Compact Disc")
{
Console.WriteLine("Drive: " drive " is a CD/DVD Drive - Skipped...");
}
else
{
Console.WriteLine("Drive: " drive " is a " DriveType " with " GBS.ToString() " GB free space.");
}
}

by Reelix on Thursday, July 30th 2009 at 02:52 AM

static void Main(string[] args)
{

Console.Title = "Storm Disk Space Mailer.";
// Store the list of drives into an array of string
string[] DriveList = Environment.GetLogicalDrives();
// Loop through the array
for (int i = 0; i < DriveList.Length; i )
{
// Show each drive
getSize(DriveList[i].Replace(":\\", ""));
}
Console.WriteLine("Listing Complete - Press enter to close.");
Console.ReadLine();
}

public static void getSize(string drive)
{
ManagementObject diskSize = new ManagementObject("win32_logicaldisk.deviceid=\"" drive ":\"");

diskSize.Get();
double GB = Convert.ToDouble(diskSize["FreeSpace"]);
GB = ((((GB / 1024) / 1024) / 1024));
string GBS = GB.ToString();
try
{
GBS = GBS.Substring(0, (GBS.IndexOf(".") 3));
}
catch
{
// Do Nothing
}
string DriveType = diskSize["DriveType"].ToString();
switch (DriveType)
{
case "0":
DriveType = "Unknown";
break;
case "1":
DriveType = "No Root Directory";
break;
case "2":
DriveType = "Removable Disk";
break;
case "3":
DriveType = "Local Disk";
break;
case "4":
DriveType = "Network Drive";
break;
case "5":
DriveType = "Compact Disc";
break;
case "6":
DriveType = "RAM Disk";
break;
default:
DriveType = "Unknown";
break;
}
if (DriveType == "Compact Disc")
{
Console.WriteLine("Drive: " drive " is a CD/DVD Drive - Skipped...");
}
else
{
Console.WriteLine("Drive: " drive " is a " DriveType " with " GBS.ToString() " GB free space.");
}
}

by Reelix on Thursday, July 30th 2009 at 02:53 AM

Ewps, Double Post :(


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons