469,326 Members | 1,562 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Finding the size of a folder

111 100+
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
Oct 29 '08 #1
5 2608
bharathreddy
111 100+
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
Oct 29 '08 #2
Frinavale
9,735 Expert Mod 8TB
Thanks for this solution Bharath. Great example of using recursion.

-Frinny
Oct 29 '08 #3
Curtis Rutland
3,256 Expert 2GB
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.
Oct 29 '08 #4
Curtis Rutland
3,256 Expert 2GB
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.
Oct 29 '08 #5
bharathreddy
111 100+
You are right!..

Thanks
Bharath Reddy VasiReddy
May 18 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Ed | last post: by
8 posts views Thread by Kai-Mikael J-Aro | last post: by
1 post views Thread by Mike Turco | last post: by
9 posts views Thread by Laurent Bugnion | last post: by
3 posts views Thread by TyBreaker | last post: by
2 posts views Thread by Eugene Anthony | last post: by
1 post views Thread by sudhivns | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.