Hi @דני שטרית, Welcome to Microsoft Q&A,
This code calculates the total space, used space, and free space in gigabytes (GB) for each fixed drive on the system. Then it calculates the used percentage and displays the information for each drive.
using System;
using System.IO;
namespace xxx
{
internal class Program
{
static void Main(string[] args)
{
// Get the list of fixed drives on the system
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.DriveType == DriveType.Fixed)
{
// Calculate consumption in fractions
double totalSpaceGB = drive.TotalSize / (1024.0 * 1024 * 1024); // Convert bytes to gigabytes
double freeSpaceGB = drive.AvailableFreeSpace / (1024.0 * 1024 * 1024); // Convert bytes to gigabytes
double usedSpaceGB = totalSpaceGB - freeSpaceGB;
// Calculate consumption in percentages
double usedPercentage = (usedSpaceGB / totalSpaceGB) * 100;
Console.WriteLine($"Drive {drive.Name}:");
Console.WriteLine($"Total Space: {totalSpaceGB:F2} GB");
Console.WriteLine($"Used Space: {usedSpaceGB:F2} GB");
Console.WriteLine($"Free Space: {freeSpaceGB:F2} GB");
Console.WriteLine($"Used Percentage: {usedPercentage:F2}%\n");
}
}
Console.ReadLine();
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.