Solved

Determing folder size using c#

Posted on 2007-12-03
11
3,776 Views
Last Modified: 2010-04-21
How can i determine size of a folder in c# once it's been selected by the folderbrowserdialog?

thanks

0
Comment
Question by:DFCRJ
  • 5
  • 5
11 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20396889
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20396915
or more simply like this
long total = 0;
foreach(System.IO.FileInfo file in dirInfo.GetFiles())
total += file.Length;
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 300 total points
ID: 20397060

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
 

Author Comment

by:DFCRJ
ID: 20397376
what would be the best way to display it in the app? tried using a label but returns a conversion error.
I
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20397514
What code did u use to get the conversion error?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:DFCRJ
ID: 20397519
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20397532
where here are u assigning the long value to a label?
0
 

Author Comment

by:DFCRJ
ID: 20397541
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
 
LVL 21

Assisted Solution

by:surajguptha
surajguptha earned 200 total points
ID: 20397550
lblsize.Text = size.ToString()
0
 

Author Comment

by:DFCRJ
ID: 20397571
Oh YEA!!!
0
 

Author Closing Comment

by:DFCRJ
ID: 31412376
Thanks to all who hung in there with a beginner
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now