- 1 year ago
Get Current System Volume (Windows 10)
To get the current system volume on Windows 10 using C#, you can use the DriveInfo
class from the System.IO
namespace. Here's an example:
csharpusing System;
using System.IO;
class Program
{
static void Main()
{
string systemDrive = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2);
DriveInfo drive = new DriveInfo(systemDrive);
long totalSpace = drive.TotalSize;
long freeSpace = drive.TotalFreeSpace;
Console.WriteLine($"System Drive: {drive.Name}");
Console.WriteLine($"Total Space: {FormatBytes(totalSpace)}");
Console.WriteLine($"Free Space: {FormatBytes(freeSpace)}");
}
static string FormatBytes(long bytes)
{
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
int suffixIndex = 0;
double size = bytes;
while (size >= 1024 && suffixIndex < suffixes.Length - 1)
{
size /= 1024;
suffixIndex++;
}
return $"{size:0.##} {suffixes[suffixIndex]}";
}
}
In this example, we first obtain the system drive's root directory using Environment.GetFolderPath(Environment.SpecialFolder.System)
. We extract the drive letter (e.g., "C:") and create a DriveInfo
object using that drive letter.
We then retrieve the total size and free space of the system drive using the TotalSize
and TotalFreeSpace
properties of the DriveInfo
object, respectively. Finally, we format the values and display them.
The FormatBytes
method is a helper method that converts bytes to a human-readable format with the appropriate suffix (e.g., KB, MB, GB).
Compile and run the code to see the current system volume information.