I'm using DirectoryInfo's GetFiles("*", SearchOption.AllDirectories) method to get all the files in a directory. Then I loop over all the files and and call file.Length to get the full size of the original directory. This directory should be increasing as the application runs.
Checking the directory size is within a periodic task that runs frequently.
Here's the strange part. When I run a test for this, I see that the directory size is never increasing when it should be. If I set a breakpoint inside the loop, I can see it being hit, but it is only reporting stale files, never showing it increase.
The really weird part is that if I set the same breakpoint and this time right click on the directory from Windows Explorer, select "Properties," and allow the application to continue, the application will recognize that the directory size is increasing and report the correct values.
I'm not sure if this detail will help, but the first time I select Properties I see the stale size, and the second time I select it, I see the accurate size. But I only need to select it once for the application to begin recognizing that it has grown.
I think this is due to some caching that I am not aware of. I've been using the Refresh() method on the DirectoryInfo object, though, to no avail.
Anyone know what's going on?
EDIT: adding code
Code for getting the directory size:
GetDirectorySize(DirectoryInfo directory)
{
directory.Refresh(); // Have tried with and without this line
long size = 0;
FileInfo[] files = directory.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
size += file.Length;
}
return size;
}