Windows systems don’t show folder sizes per default. One need to either use a third party software or make a script or a program yourself. In this tutorial I will show how to create a simple C# application in Visual Studio that shows folder sizes on your Windows machine.
I’m going to use Visual Studio 2013 and create a simple Forms application:
In the Form Designer we create two text boxes and a button “Choose”:
We make the simplest application that will bring up the folder browsing dialog when the user clicks the button “Choose”. After the user chooses the folder it will appear in the second text box and show the size of this folder.
In order to choose the folder in the file browsing dialog we can use the FolderBrowserDialog class in C#.
Create an event for the “Choose” button by clicking on it in the Form Designer. Fill in the function with the code to choose the folder and show the results in the text box.
Here’s a sample C# code using FolderBrowserDialog
function:
private void button1_Click(object sender, EventArgs e) { var dialog = new System.Windows.Forms.FolderBrowserDialog(); DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { textBox1.Text = dialog.SelectedPath; printFolderSize(dialog.SelectedPath); } }
We didn’t give any new names to the Form items for the purposes of this tutorial:
textBox1
– the smallest text box at the top of the form. It will show what folder the user has chosen.richTextBox1
– the biggest textarea where the result of the size calculation will appear.button1
– the “Choose” button.Create a function called printFolderSize
with a string as an input. Test it by printing the chosen folder:
private void printFolderSize(string folder) { richTextBox1.AppendText(folder); richTextBox1.AppendText("n"); }
In order to calculate the size of the folder we need:
Recursive function is a good solution in this case. Recursive function is a function that calls itself. It is also called a direct recursion. One should be careful to use these functions as they can create endless loops within the application.
To find how much space a single file takes we use the FileInfo class in C#:
FileInfo[] files = directoryInfo.GetFiles(); foreach (FileInfo file in files) { size += file.Length; }
To explore all the folders in a directory, we use the DirectoryInfo class in C#. Create a new function getDirSize
with DirectoryInfo as an input. The function will look like this:
private static long getDirSize(DirectoryInfo d) { long size = 0; FileInfo[] files = d.GetFiles(); foreach (FileInfo file in files) { size += file.Length; } DirectoryInfo[] directories = d.GetDirectories(); foreach (DirectoryInfo dir in directories) { size += getDirSize(dir); } return size; }
The calculated folder size is a long
value and shows how much space the folder takes in bytes. If we print the result at this stage we will get something like this:
The number 1148966285 bytes is not the same as 1,1 GB. Remember, that there are 1024 bytes in one KB.
This is how many bytes it is in a kilobyte, megabyte, gigabyte and so on:
For the purpose of this tutorial we write a simple conversion:
if(bytes_size > 1073741824) { richTextBox1.AppendText("" + bytes_size / 1073741824 + " GB "); } else if (bytes_size > 1048576) { richTextBox1.AppendText("" + bytes_size / 1048576 + " MB "); } else if (bytes_size > 1024) { richTextBox1.AppendText("" + bytes_size / 1024 + " KB "); } else { richTextBox1.AppendText("" + bytes_size + " B "); }
Now the result is easy to read:
The code in this tutorial is not “bullet proof”. For example, when I have chosen the folder that contained files with restricted access, an exception was thrown:
Visual Studio Debugger tells us that the exception appeared while trying to access files from a chosen directory. An exception handling should be implemented.
As we see our program won’t give an absolutely accurate result for how much space a folder takes as it won’t have access to the restricted files.
The whole code for this tutorial is simple. It can be used to build up the application for your own needs.
using System; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace DirectorySize { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var dialog = new System.Windows.Forms.FolderBrowserDialog(); DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { textBox1.Text = dialog.SelectedPath; printFolderSize(dialog.SelectedPath); } } private void printFolderSize(string folder) { long bytes_size = getDirSize(new DirectoryInfo(folder)); if(bytes_size > 1073741824) { richTextBox1.AppendText("" + bytes_size / 1073741824 + " GB "); } else if (bytes_size > 1048576) { richTextBox1.AppendText("" + bytes_size / 1048576 + " MB "); } else if (bytes_size > 1024) { richTextBox1.AppendText("" + bytes_size / 1024 + " KB "); } else { richTextBox1.AppendText("" + bytes_size + " B "); } richTextBox1.AppendText(folder); richTextBox1.AppendText("n"); } private static long getDirSize(DirectoryInfo d) { long size = 0; FileInfo[] files = d.GetFiles(); foreach (FileInfo file in files) { size += file.Length; } DirectoryInfo[] directories = d.GetDirectories(); foreach (DirectoryInfo dir in directories) { size += getDirSize(dir); } return size; } } }
Senior Software Engineer developing all kinds of stuff.