0

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;
}
bronzeson
  • 71
  • 1
  • 8
  • Are files changing size? Or are more files being added to the directory? – Sam Axe Nov 06 '19 at 00:19
  • 2
    This may be of interest: https://stackoverflow.com/questions/6333891/caching-fileinfo-properties-in-c-sharp – Sam Axe Nov 06 '19 at 00:21
  • 1
    On a side note, use `Directory.EnumerateFiles()` instead of `Directory.GetFiles()`. It tends to be faster. – Sach Nov 06 '19 at 00:25
  • I suspect the files are being actively written do. The directory entry is not updated until the handle is closed. Viewing the Properties opens a temporary handle and closes it, and it's that "close" that updates the directory entry. – Raymond Chen Nov 06 '19 at 00:46
  • You say, *"This directory should be increasing as the application runs."*, but don't explain why. It would be helpful to know why/how the directory size is increasing. Are you manually adding files, or is your code adding files? If it's your code, it would be nice if you included it so we get a sample that reproduces the problem (running your sample above does not reproduce the problem on my machine). – Rufus L Nov 06 '19 at 01:26
  • Side note: your method can be reduced to one line: `return directory?.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length) ?? 0;` – Rufus L Nov 06 '19 at 01:28

0 Answers0