Determing folder size using c#

How can i determine size of a folder in c# once it's been selected by the folderbrowserdialog?

thanks

DFCRJAsked:
Who is Participating?
 
Daniel JungesConnect With a Mentor Commented:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\tmp");
long size = getDirSize(dir);

public long getDirSize(System.IO.DirectoryInfo dir)
        {
            long size = 0;
            System.IO.DirectoryInfo[] dirs = dir.GetDirectories();
            System.IO.FileInfo[] files = dir.GetFiles();

            foreach (System.IO.FileInfo fi in files)
                size += fi.Length;

            foreach (System.IO.DirectoryInfo di in dirs)
                size += getDirSize(di);
            return size;
        }

0
 
surajgupthaCommented:
0
 
surajgupthaCommented:
or more simply like this
long total = 0;
foreach(System.IO.FileInfo file in dirInfo.GetFiles())
total += file.Length;
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DFCRJAuthor Commented:
what would be the best way to display it in the app? tried using a label but returns a conversion error.
I
0
 
surajgupthaCommented:
What code did u use to get the conversion error?
0
 
DFCRJAuthor Commented:
Sorry -

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\tmp");
long size = getDirSize(dir);

public long getDirSize(System.IO.DirectoryInfo dir)
        {
            long size = 0;
            System.IO.DirectoryInfo[] dirs = dir.GetDirectories();
            System.IO.FileInfo[] files = dir.GetFiles();

            foreach (System.IO.FileInfo fi in files)
                size += fi.Length;

            foreach (System.IO.DirectoryInfo di in dirs)
                size += getDirSize(di);
            return size;
        }
0
 
surajgupthaCommented:
where here are u assigning the long value to a label?
0
 
DFCRJAuthor Commented:
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\tmp");
long size = getDirSize(dir);
lblsize = size;

amongst other places as well. I'm learning.
0
 
surajgupthaConnect With a Mentor Commented:
lblsize.Text = size.ToString()
0
 
DFCRJAuthor Commented:
Oh YEA!!!
0
 
DFCRJAuthor Commented:
Thanks to all who hung in there with a beginner
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.