C# Program to Calculate the Size of Folder

This is a C# Program to calculate the size of folder.

Problem Description

This C# Program Calculates the Size of Folder.

Problem Solution

Here the program calculates the size of the folder including its subfolders and hence display the size in bytes, kilobytes and MB.

Program/Source Code

Here is source code of the C# Program to Calculate the Size of Folder. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Calculate the Size of Folder
 */
using System;
using System.Linq;
using System.IO;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dInfo = new DirectoryInfo(@"C:/sri");            
            long sizeOfDir = DirectorySize(dInfo, true);
            Console.WriteLine("Directory size in Bytes : " +
            "{0:N0} Bytes", sizeOfDir);
            Console.WriteLine("Directory size in KB : " +
            "{0:N2} KB", ((double)sizeOfDir) / 1024);
            Console.WriteLine("Directory size in MB : " +
            "{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));
            Console.ReadLine();
        }
        static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
        {            
            long totalSize = dInfo.EnumerateFiles()
                         .Sum(file => file.Length);            
            if (includeSubDir)
            {                
                totalSize += dInfo.EnumerateDirectories()
                         .Sum(dir => DirectorySize(dir, true));
            }
            return totalSize;
        }
    }
}
Program Explanation

This C# program is used to calculate the size of folder. In DirectorySize() function the EnumerateFiles() function is used to return an enumerable collection of file names that match a search pattern in a specified path, and optionally searches subdirectories.

advertisement
advertisement

The EnumerateDirectories() function is used to return an enumerable collection of directory names in a specified path. Calculate the size of the folder including its subfolders and print the size in bytes, kilobytes and MB.

Runtime Test Cases
 
Directory Size in Bytes : 1,482 Bytes
Directory Size in KB : 1.45 KB
Directory Size in MB : 0.00 MB

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.