How can I get the list of logial drives (C#) on a system as well as their capacity and free space?
Asked
Active
Viewed 6.6k times
7 Answers
70

Richard
- 106,783
- 21
- 203
- 265
-
Is this something new that was added in the latest version of .NET. I wrote a small app to display this years ago but had to go the WMI route at the time. Very handy to know anyway... cheers – Eoin Campbell Apr 23 '09 at 14:18
-
Quick look on MSDN: was added in .NET 2.0. – Richard Apr 23 '09 at 14:22
36
foreach (var drive in DriveInfo.GetDrives())
{
double freeSpace = drive.TotalFreeSpace;
double totalSpace = drive.TotalSize;
double percentFree = (freeSpace / totalSpace) * 100;
float num = (float)percentFree;
Console.WriteLine("Drive:{0} With {1} % free", drive.Name, num);
Console.WriteLine("Space Remaining:{0}", drive.AvailableFreeSpace);
Console.WriteLine("Percent Free Space:{0}", percentFree);
Console.WriteLine("Space used:{0}", drive.TotalSize);
Console.WriteLine("Type: {0}", drive.DriveType);
}

Community
- 1
- 1

Chris Ballance
- 33,810
- 26
- 104
- 151
25
Their example has more robust, but here's the crux of it
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
You could also P/Invoke and call the win32 function (or use it if you're in unmanaged code).
That only gets a list of the drives however, for information about each one, you would want to use GetDrives as Chris Ballance demonstrates.

Maytham Fahmi
- 31,138
- 14
- 118
- 137

Tom Ritter
- 99,986
- 30
- 138
- 174
7
maybe this is what you want:
listBox1.Items.Clear();
foreach (DriveInfo f in DriveInfo.GetDrives())
listBox1.Items.Add(f);

Ondrej Janacek
- 12,486
- 14
- 59
- 93

mehrdad
- 71
- 1
- 1
2
This is a wonderful piece of code.
ObjectQuery query =
new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"); // Create query to select all the hdd's
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query); // run the query
ManagementObjectCollection queryCollection = searcher.Get(); // get the results
string sVolumeLabel = "";
string[,] saReturn = new string[queryCollection.Count, 7];
int i = 0; // counter for foreach
foreach (ManagementObject m in queryCollection)
{
if (string.IsNullOrEmpty(Convert.ToString(m["VolumeName"]))) { sVolumeLabel = "Local Disk"; } else { sVolumeLabel = Convert.ToString(m["VolumeName"]); } // Disk Label
string sSystemName = Convert.ToString(m["SystemName"]); // Name of computer
string sDriveLetter = Convert.ToString(m["Name"]); // Drive Letter
decimal dSize = Math.Round((Convert.ToDecimal(m["Size"]) / 1073741824), 2); //HDD Size in Gb
decimal dFree = Math.Round((Convert.ToDecimal(m["FreeSpace"]) / 1073741824), 2); // Free Space in Gb
decimal dUsed = dSize - dFree; // Used HDD Space in Gb
int iPercent = Convert.ToInt32((dFree / dSize) * 100); // Percentage of free space
saReturn[i,0] = sSystemName;
saReturn[i,1] = sDriveLetter;
saReturn[i,2] = sVolumeLabel;
saReturn[i,3] = Convert.ToString(dSize);
saReturn[i,4] = Convert.ToString(dUsed);
saReturn[i,5] = Convert.ToString(dFree);
saReturn[i,6] = Convert.ToString(iPercent);
i++; // increase counter. This will add the above details for the next drive.
}

SpoiledTechie.com
- 10,515
- 23
- 77
- 100
2
You can retrieve this information with Windows Management Instrumentation (WMI)
using System.Management;
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
// Loop through each object (disk) retrieved by WMI
foreach (ManagementObject moDisk in mosDisks.Get())
{
// Add the HDD to the list (use the Model field as the item's caption)
Console.WriteLine(moDisk["Model"].ToString());
}
Theres more info here about the attribute you can poll
http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

Eoin Campbell
- 43,500
- 17
- 101
- 157
-
Can't get this working on my computer. System.Management doesn't have ManagementObjectSearcher class now. The URL is also not pointing to a valid web page. – Sheo Narayan Jul 06 '16 at 11:22
-
You need to add reference for that. On Visual Studio, Right click on project then go to Add -> Reference. Then, search for "System.Management" and add it. – Gippeumi Dec 23 '16 at 12:23
1
I deployed an SSIS package and was getting an error simply because on Ballance example we are reading the drives without waiting for them to be ready:
string EmailMessage = "";
foreach (var drive in System.IO.DriveInfo.GetDrives())
{
if (drive.IsReady == true) /*Make sure we can read*/
{
double freeSpace = drive.TotalFreeSpace;
double totalSpace = drive.TotalSize;
double percentFree = (freeSpace / totalSpace) * 100;
float num = (float)percentFree;
//MessageBox.Show("Drive:" + drive.Name + " With " + num + "% free.");
if (num < 5)
{
EmailMessage = "Drive:" + drive.Name + " With " + num + "% free.";
}
}
}

Leonel Gonzalez
- 89
- 7