0

I have a folder being watched by a service with a FileSystemWatcher and I used this Answer to help me for large files being written to it.

However, I came to an issue if there are consecutive file creations about(100MB or more).

Q: How would can I tackle this issue? Like example around 10 files (100MB each) written to my folder.

Note: This folder is accessed through my network. There could be files created but not really complete/finished. the FileSystemWatcher could process the file without it being written completely.

Current code for checking if opening file throws an exception

private static bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;
        try
        {
            stream = file.Open(FileMode.Open,
                     FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }

Current code checking if there is a file created.

    private void FSWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        var files = getFiles(FSWatcher.Path); //stores all filenames in the directory in a list
        if (files.Count() > 0)
        {
            foreach (string file in files)
            {
                FileInfo file_info = new FileInfo(file);
                while (IsFileLocked(file_info))
                {
                    Thread.Sleep(5000); //Sleep if file unavailable
                }
                //code to process the file
            }
            //some code here
        }
    }
Community
  • 1
  • 1
Hexxed
  • 683
  • 1
  • 10
  • 28

1 Answers1

0

The way I've handled watching folders in the past used polling instead of FileSystemWatcher, its not 100% reliable anyway

The processor I made looped through every file and tried to move them into a "Processing" directory first. The move will (probably*) only succeed if the file has finished being written, and so once the move succeeds the thread that moved it can safely trigger processing of that file (e.g. in a new thread). Note that a file can only be moved once which means you can't accidentally have two different threads attempt to process a file at the same time.

*: Its possible (but rare) for an application to create a file which can be moved while its still open

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367