You need to calculate the total size in bytes of all files in a directory. This can be useful for compression or file transfers. An example problem is determining the size of a directory before it is compressed, and after it is compressed. Here we use the Directory class to find the total byte size of the directory, using the C# programming language.

We can use the Directory class and a loop. The .NET Framework does not provide one method to determine the directory size, so we must write our own custom method. Here we sum the length of each file, returning the total size in bytes.
Program that gets directory size [C#]
using System;
using System.IO;
class Program
{
static void Main()
{
Console.WriteLine(GetDirectorySize("C:\\Site\\"));
}
static long GetDirectorySize(string p)
{
// 1
// Get array of all file names.
string[] a = Directory.GetFiles(p, "*.*");
// 2
// Calculate total bytes of all files in a loop.
long b = 0;
foreach (string name in a)
{
// 3
// Use FileInfo to get length of each file.
FileInfo info = new FileInfo(name);
b += info.Length;
}
// 4
// Return total size
return b;
}
}
Output
It outputs a number that is equal to the total number of bytes in the folder.Overview. It is static. Static methods do not need to save state, and this method doesn't save any state information. In step 1, it calls Directory.GetFiles(). This gets all the files matching the "filter" at the directory in the path specified. It fills a string array with all the file names.
Static Method String Array
In step 2, it loops over each file. The foreach loop looks through each file name and then in step 3 creates a new FileInfo object. This object allows us to easily find certain "info" properties of the file. In step 4, it returns the sum of all the bytes in the first level of the folder.
Foreach Loop ExamplesI tested my "C:\\site" directory, which is a staging directory for my web projects. As seen in the screenshot above, the "site" directory contains 2.88 MB (3,029,971 bytes). I then ran the above console program, and it returned the same byte length.

Can I only get the size of all PNG files? Yes. You will need to change the filter specified in the Directory.GetFiles() call to "*.png". Often there are unimportant files you will want to exclude in the size calculations.
How can I recurse through all folders? Combine the method shown here with a recursive file-finding method, like the one in the linked article. For the purposes of the program I used this code in, that logic wouldn't be useful, so I omitted it here.
Recursive File ListHere we note how you can use this method with the FolderBrowserDialog control in Windows Forms. Use the path returned from the dialog, after DialogResult.OK, and pass it to the method above. The following Windows Forms example shows how this is done. This site now contains a useful tutorial on the FolderBrowserDialog control, which can help with getting this working.
FolderBrowserDialog ControlCode fragment [C#]
// Show the folder browser.
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
// Path specified.
string folder = folderBrowserDialog1.SelectedPath;
// Calculate total size of all pngs.
long directorySize = GetDirectorySize(folder);
}
Here we saw how you can measure directory sizes in bytes. Use a combination of Directory methods and FileInfo objects to sum the bytes. Additionally, you can use this method with a FolderBrowserDialog or other UI in Windows Forms and the C# programming language.
GetFileSystemInfos Method FileInfo Examples File Handling