How to calculate Folder size in C# WindowsForms

Andi 111 Reputation points
2022-10-14T09:34:30.273+00:00

Hi i am already able to calculate the size of Files but i want to calulate the size of a folder on a similar way and with the conversion.

How can i convert my actual programm to easily calulate folder sizes?

FileInfo di = new FileInfo(@"C:\Users\icon.ico");

                if (di.Length >= 1073741824.0)  
                {  
                    textBox13.Text = String.Format("{0:##.##}", di.Length / 1073741824.0) + " GB";  
                }  
                else if (di.Length >= 1048576.0)  
                {  
                    textBox13.Text = String.Format("{0:##.##}", di.Length / 1048576.0) + " MB";  
                }  
                else if (di.Length >= 1024.0)  
                    textBox13.Text = String.Format("{0:##.##}", di.Length / 1024.0) + " KB";  
                else if (di.Length > 0 && di.Length < 1024.0)  
                    textBox13.Text = di.Length.ToString() + " KB";  

Thanks for your help in advice!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 83,206 Reputation points
    2022-10-14T11:44:53.967+00:00

    You can see the sample with NtQueryDirectoryFile I had posted in this thread
    (you can find simpler methods, a bit slower, like the samples from IronRazerz in this thread (in VB, but same code in C#))

    0 comments No comments