Solved

Determing folder size using c#

Posted on 2007-12-03
11
3,780 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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