Size of all files in a folder

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
 
D

Danny Tuppeny

**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!
 
D

**Developer**

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!
 
P

Piotrek

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
 
W

William Stacey [MVP]

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;
}
 
W

William Stacey [MVP]

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;
}
 

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