DirectoryInfo....folder size??

  • Thread starter Thread starter JJ
  • Start date Start date
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
 
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.
 
Back
Top