473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2828
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

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

Similar topics

9
by: Ed | last post by:
Is there any known issue with the use of "Scripting.FileSystemObject"? Here's a snippet of my code to obtain the filesize of a file Set fso = CreateObject("Scripting.FileSystemObject") Set...
8
by: Kai-Mikael Jää-Aro | last post by:
AIUI, there are no global style defaults for HTML tags, i e the W3C does not enforce that e g an <H1> should have a specific type-face, size, and so on, but rather this is decided by the browser. ...
1
by: Mike Turco | last post by:
How do I go about finding the location of the desktop, temp folder, program files folder, my documents, etc. For example, say that I want to programmatically export a table to a file on the...
9
by: Laurent Bugnion | last post by:
Hi, I am wondering what is the best way to find out which ASP.NET sessions are still active. Here is the reason: I have a custom control which can upload files. It saves the files in a folder...
3
by: TyBreaker | last post by:
I have a vbscript which returns the folder size of any folder instantly by referring to Folder.Size. I don't need any iteration or recursion through the subfolders, adding together individual file...
2
by: Eugene Anthony | last post by:
I am having a problem with this code. The problem I am facing is that the first image is displayed in its original size. But when I click the next button, the next image not displayed in its...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
1
by: sudhivns | last post by:
There is a List of Files/Folders maintained. Need to calculate the total size(file/Folder) of this list. Need to calculate file size such that any nested file/folder is there along with parent in the...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.