Hi as a stepping stone to what i'm trying to do i've decided to display
the size of all files within any given directory, including sub
directories. However it isn't working as expected, please find code
below and description of what's happening - any advice would be very
appreciated. TIA. Gary.
I'm using:
using System;
using System.IO;
namespace dirlist_cs
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// ListDirectory(new DirectoryInfo(@"c:\Program Files"));
// FullDirList(new DirectoryInfo(@"c:\program files\kru"),
"*.*");
string sizes = FileSizes(new DirectoryInfo(@"c:\George"),
"*.*").ToString();
Console.Write(sizes);
}
static long FileSizes(DirectoryInfo dir, string searchpattern)
{
long size = 0;
foreach (FileInfo f in dir.GetFiles(searchpattern))
{
size += (f.Length);
}
// process each directory
foreach (DirectoryInfo d in dir.GetDirectories())
{
FileSizes(d, searchpattern);
}
return size;
}
I had hoped that this would return the size of a given directory by
enumerating through the sizes of the files in the directory and it's
sub directories. But it's giving differing results.
In windows explorer, if I select the properties of a folder it is
displayed as:
Size: 4.01MB (4,213,751 bytes)
Size on disk: 4.07MB (4,276,224 bytes)
Yet console output from the above is: 2982849
Can someone explain to me what's going wrong please?