Computing the size of a folder
The .Net framework does not provide any function to compute the size of all the files in a folder (and its subfolders). As it is not very hard to do here's the code:
C#
public static long FolderSize(string path)
{
long size = 0;
DirectoryInfo directoryInfo = new DirectoryInfo(path);
IEnumerable<FileInfo> files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
size += file.Length;
}
return size;
}
Do you have a question or a suggestion about this post? Contact me!
Enjoy this blog?💖 Sponsor on GitHub