Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate/determine Folder Size in .NET?

Tags:

c#

.net

I am creating application in Winform which among other things will also need to have the ability to calculate size of the folder.

Can someone give me pointers how to do that?

thanks

like image 782
user850010 Avatar asked Dec 01 '22 02:12

user850010


1 Answers

I use the following extension method to do that:

    public static long Size(this DirectoryInfo Directory, bool Recursive = false)
    {
        if (Directory == null)
            throw new ArgumentNullException("Directory");
        return Directory.EnumerateFiles("*", Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Sum(x => x.Length);
    }
like image 184
JaCraig Avatar answered Dec 04 '22 20:12

JaCraig