Check consumtion of fixed drives memory in fraction + percentages

Dani_S 4,501 Reputation points
2024-04-07T16:07:13.1866667+00:00

Hi,

Can you supply code how to caculate consumtion of fixed drives memory in fraction + percentages.

Thanks,

Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-04-08T03:11:34.9933333+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.