Connecting Tech Pros Worldwide Forums | Help | Site Map

Finding the size of a folder

Member
 
Join Date: Aug 2006
Location: USA
Posts: 93
#1: Oct 29 '08
How to find the size of a folder using C# code?

step1:
Here take the folder which u want to find the size, then pass that folder to the recursive function name FolderSize!.

Expand|Select|Wrap|Line Numbers
  1. DirectoryInfo dirInfo = new DirectoryInfo(strPath) ;            
  2. decimal dSize = 0;
  3. //If the folder exists then only its size will be calculated if not its size will be 
  4. //zero!
  5.  if (dirInfo.Exists)
  6.                 dSize = FolderSize(dirInfo);
  7.  
Step2:

Expand|Select|Wrap|Line Numbers
  1. #region FolderSize (Recursive Function)
  2.     /// <summary>
  3.     /// This method is used to find the size of the folder specified.
  4.     /// This method is called recursively to find the length of the folder.
  5.     /// </summary>
  6.     /// <param name="dirInfo"></param>
  7.     /// <returns></returns>
  8.     protected decimal FolderSize(DirectoryInfo dirInfo)
  9.     {
  10.         decimal dSize = 0;
  11.  
  12.         //Size of the files in that specific fodler.
  13.         foreach (FileInfo fileInfo in dirInfo.GetFiles())
  14.         {
  15.             dSize += Math.Abs((decimal)(fileInfo.Length / 1024.0));
  16.         }
  17.  
  18.         //Size of the files in the child folders
  19.         foreach (DirectoryInfo childDir in dirInfo.GetDirectories())
  20.         {
  21.             dSize += FolderSize(childDir);
  22.         }
  23.         return dSize;
  24.     }
  25.     #endregion
This is the simple recursive method used for finding the size of the folder hirarchy!.

Thanks & Regards
Bharath Reddy VasiReddy

Member
 
Join Date: Aug 2006
Location: USA
Posts: 93
#2: Oct 29 '08

re: Finding the size of a folder


If u want to round off the value to a two digit after the point, use the below statement instead of dSize = FolderSize(dirInfo);

Expand|Select|Wrap|Line Numbers
  1.  //Round to the two decimal digit.            
  2.  dSize = Math.Round(FolderSize(dirInfo),2);
  3.  
Thanks & Regards
Bharath Reddy VasiReddy
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: Oct 29 '08

re: Finding the size of a folder


Thanks for this solution Bharath. Great example of using recursion.

-Frinny
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Oct 29 '08

re: Finding the size of a folder


Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

Also, this forum is for questions+answers only. We do have an articles section. If you post in the Editor's Corner, we can review your article, and move it to the articles section.

I'll move it there now.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#5: Oct 29 '08

re: Finding the size of a folder


I have a suggestion for you:

It doesn't have to be recursive at all, if you use the overloaded version of GetFiles:
Expand|Select|Wrap|Line Numbers
  1. dirInfo.GetFiles("*", SearchOption.AllDirectories);
  2.  
That does the recursive part for you.
Member
 
Join Date: Aug 2006
Location: USA
Posts: 93
#6: May 18 '09

re: Finding the size of a folder


You are right!..

Thanks
Bharath Reddy VasiReddy
Reply