Solved

Determing folder size using c#

Posted on 2007-12-03
11
3,778 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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