C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We display the values returned by the properties on the DriveInfo instance. The free space properties return long values.
LongOutput: The free space numbers are in bytes. It is possible to convert these to megabytes and gigabytes using custom helper methods.
Convert Bytes, MegabytesC# program that uses DriveInfo
using System;
using System.IO;
class Program
{
static void Main()
{
DriveInfo info = new DriveInfo("C");
// [1] Print sizes.
Console.WriteLine(info.AvailableFreeSpace);
Console.WriteLine(info.TotalFreeSpace);
Console.WriteLine(info.TotalSize);
Console.WriteLine();
// [2] Format and type.
Console.WriteLine(info.DriveFormat);
Console.WriteLine(info.DriveType);
Console.WriteLine();
// [3] Name and directories.
Console.WriteLine(info.Name);
Console.WriteLine(info.RootDirectory);
Console.WriteLine(info.VolumeLabel);
Console.WriteLine();
// [4] Ready.
Console.WriteLine(info.IsReady);
}
}
Output
682166767616
682166767616
984045580288
NTFS
Fixed
C:\
C:\
OS
True
Here: We use the foreach-loop on the result of the GetDrives method. The code from the first example could be added to the loop.
ForeachC# program that gets all drives
using System;
using System.IO;
class Program
{
static void Main()
{
// Print all drive names.
var drives = DriveInfo.GetDrives();
foreach (DriveInfo info in drives)
{
Console.WriteLine(info.Name);
}
}
}
Output
C:\
D:\