C# Get directory size using LINQ, c# GetDirectorySize

C# Get directory size using LINQ, DirectorySize

Sometimes you need or must know the folder DirectorySize or frin more directories in C#.
In this case you can use this method, which get the folder size in c#, the output size is in bytes.

public static long DirectorySize(string parentDirectory)
{
return new DirectoryInfo(parentDirectory).GetFiles(searchPattern: "*", searchOption: SearchOption.AllDirectories).Sum(file => file.Length);
}

In this linq method you can also set an search pattern and choose if only TopDirectories or AllDirectories will be run through.

Comments

comments

Kommentar verfassen