Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to calculate the size of a directory in .NET?

Tags:

c#

.net

windows

I've written the following routine to manually traverse through a directory and calculate its size in C#/.NET:

  protected static float CalculateFolderSize(string folder) {     float folderSize = 0.0f;     try     {         //Checks if the path is valid or not         if (!Directory.Exists(folder))             return folderSize;         else         {             try             {                 foreach (string file in Directory.GetFiles(folder))                 {                     if (File.Exists(file))                     {                         FileInfo finfo = new FileInfo(file);                         folderSize += finfo.Length;                     }                 }                  foreach (string dir in Directory.GetDirectories(folder))                     folderSize += CalculateFolderSize(dir);             }             catch (NotSupportedException e)             {                 Console.WriteLine("Unable to calculate folder size: {0}", e.Message);             }         }     }     catch (UnauthorizedAccessException e)     {         Console.WriteLine("Unable to calculate folder size: {0}", e.Message);     }     return folderSize; }  

I have an application which is running this routine repeatedly for a large number of folders. I'm wondering if there's a more efficient way to calculate the size of a folder with .NET? I didn't see anything specific in the framework. Should I be using P/Invoke and a Win32 API? What's the most efficient way of calculating the size of a folder in .NET?

like image 813
Steve Wranovsky Avatar asked Jan 22 '09 05:01

Steve Wranovsky


People also ask

How do I get the size of a directory in C#?

To calculate the size of a folder in C#, use the Directory. EnumerateFiles Method and get the files. Creates all directories and subdirectories in the specified path unless they already exist. Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.

How do you determine size of folder?

Go to Windows Explorer and right-click on the file, folder or drive that you're investigating. From the menu that appears, go to Properties. This will show you the total file/drive size. A folder will show you the size in writing, a drive will show you a pie chart to make it easier to see.

How do I find the size of a directory in DOS?

You will want to use dir /a/s so that it includes every file, including system and hidden files. This will give you the total size you desire.

How do I find the size of a directory in Linux?

To get the total size of a directory in Linux, you can use the du (disk-usage) command.


2 Answers

No, this looks like the recommended way to calculate directory size, the relevent method included below:

public static long DirSize(DirectoryInfo d)  {         long size = 0;         // Add file sizes.     FileInfo[] fis = d.GetFiles();     foreach (FileInfo fi in fis)      {               size += fi.Length;         }     // Add subdirectory sizes.     DirectoryInfo[] dis = d.GetDirectories();     foreach (DirectoryInfo di in dis)      {         size += DirSize(di);        }     return size;   } 

You would call with the root as:

Console.WriteLine("The size is {0} bytes.", DirSize(new DirectoryInfo(targetFolder)); 

...where targetFolder is the folder-size to calculate.

like image 104
hao Avatar answered Oct 13 '22 01:10

hao


DirectoryInfo dirInfo = new DirectoryInfo(@strDirPath); long dirSize = await Task.Run(() => dirInfo.EnumerateFiles( "*", SearchOption.AllDirectories).Sum(file => file.Length)); 
like image 20
Trikaldarshiii Avatar answered Oct 13 '22 01:10

Trikaldarshiii