DirectoryInfo....folder size??

J

JJ

How do I get the immediate subdirectories and their size under a given
directory?
I just want list of folders only one level down and total size of each
folder under the parent directory?

Thanks
JJ
 
C

Claudio Grazioli

How do I get the immediate subdirectories and their size under a given
directory?
I just want list of folders only one level down and total size of each
folder under the parent directory?

Thanks
JJ

To get the names of all directories in root directory:

private void FillDirectories_Click(object sender, System.EventArgs e)
{
_txtDirectories.Text = string.Empty;
foreach(string subDirectory in Directory.GetDirectories(@"c:\"))
{
_txtDirectories.Text += subDirectory + "; ";
}
}

To get the size of a folder: There is no such thing. You have to iterate
ofer all files in your directory and the files of all subdirectories and so
on and add the size of each file.

Something like this (not tested):

public long Size(System.IO.DirectoryInfo dirInfo)
{
long total = 0;

foreach(System.IO.FileInfo file in dirInfo.GetFiles())
total += file.Length;

foreach(System.IO.DirectoryInfo dir in dirInfo.GetDirectories())
total += Size(dir);

return total;
}

But this is very very slow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top