Size of all files in a folder

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

Is there an easy way (i.e., not checking each file and adding the sizes) to
get the total size of all files in a directory tree?

And/or the number of files in the tree.



Thanks in advance
 
**Developer** said:
Is there an easy way (i.e., not checking each file and adding the sizes)
to get the total size of all files in a directory tree?

And/or the number of files in the tree.

I don't know for sure, but I'm guessing the answer is no. If you open the
Properties dialog of a big folder in Windows, the size counts up, so even
Windows must be recursing! The final result couldn't really be exposed as a
property, because you wouldn't want your app blocking while it adds them up!
 
Guess that make sense. Too bad.
Thanks

Danny Tuppeny said:
I don't know for sure, but I'm guessing the answer is no. If you open the
Properties dialog of a big folder in Windows, the size counts up, so even
Windows must be recursing! The final result couldn't really be exposed as
a property, because you wouldn't want your app blocking while it adds them
up!
 
I haven't compiled it but it should be like this:

private long objSize(System.IO.DirectoryInfo dirInfo)
{
long total=0;

foreach(FileInfo file in dirInfo.GetFiles())
total += file.Length;
foreach(DirectoryInfo dir in dirInfo.GetDirectgories())
total+=objSize(dir);
return total;
}

i've been writing from memory so sory for mistakes.
PK
 
This might also work:

DirectoryInfo di = new DirectoryInfo(@"c:\dir1");
FileInfo[] files = di.GetFiles("*",
SearchOption.AllDirectories);
long size =0;
foreach(FileInfo fi in files)
{
size += fi.Length;
}
 
Probably a 2.0 thing.

--
William Stacey [MVP]

**Developer** said:
I couldn't find any reference to SearchOption.AllDirectories

William Stacey said:
This might also work:

DirectoryInfo di = new DirectoryInfo(@"c:\dir1");
FileInfo[] files = di.GetFiles("*",
SearchOption.AllDirectories);
long size =0;
foreach(FileInfo fi in files)
{
size += fi.Length;
}
 
Back
Top