0

I am using a FileSystemWatcher to find any created directories in a directory. Specifically, I use another function to find whether or not the file created is a directory, and if it is a directory, I pass the file path onto another function that checks the size of the directory.

i.e., Directory created > check if it's a directory > if it is, check the size of the directory.

Directory file size function:

    long DirectorySize(DirectoryInfo d)
    {
        long Size = 0;
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis)
        {
            Size += fi.Length;
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis)
        {
            Size += DirectorySize(di);
        }
        return (Size);
    }

And the simple check for discovering if a file is a directory:

    bool isDirectory(string pathName)
    {
        // get the file attributes for file or directory
        FileAttributes attr = File.GetAttributes(pathName);

        //detect whether its a directory or file
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            return true;
        else
            return false;
    }

However there's an issue. I want the file to be completely written before I perform the check file size function. Once, and only once, the file is completely written should the check be performed.

I know that code exists such as this, however it's slightly off of the topic of what I want to do, because I don't want to open the file, only find its size. Would there be a way to check that the file has completely written before checking its size?

Community
  • 1
  • 1
Nick Bull
  • 9,518
  • 6
  • 36
  • 58
  • You should be able to use the code provided in the answer here (http://stackoverflow.com/questions/17612800/c-sharp-detect-if-file-has-finished-being-written) – Kevin Jan 17 '14 at 14:18
  • I've already sourced this code in the question. Somebody aptly pointed out that as soon as the check is performed, another process may lock the file instantaneously afterwards, leading to the opportunity for a race condition. Would that not affect the result of this code? – Nick Bull Jan 17 '14 at 14:21
  • And FileSystemWatcher can be unreliable because the Windows buffer overflows and it misses messages. To quote a comment on the thread the source comes from (another thread beyond that one) "There is no 100% safe way to "learn if a file is in use" because milliseconds after you do the check, the file may not be in use anymore, or vice versa. Instead, you just open the file and use it if there are no exceptions" -ssg – Kevin Jan 17 '14 at 15:08
  • "Completely written" is a very vague definition. You of course cannot know when a process has finished writing a file and is no longer going to append more data to it. The only reasonable approach is *assuming* it is completely written when you can obtain a lock on the file. Create a FileStream to open the file with FileShare.Read. If that doesn't throw an exception then you can assume that the process is not writing to the file anymore. Do note that this almost always throws when you get the Change notification. – Hans Passant Jan 17 '14 at 16:34

0 Answers0