C # FileSystemWatcher how to find out if a file is fully copied to the view folder

I am developing a .net application where I use the FileSystemWatcher class and attach its generated event in a folder. I need to take action on this event (for example, copy a file to another location). When I put a large size in the attached folder with the clock, the event rises immediately, even the process of copying files is still not completed. I do not want to check this with the file.open method.

Is there any way to find out that my process of copying files to the clock folder has been completed, and then my event caught fire.

+5
source share
7 answers

, FileSystemWatcher ( API ReadDirectoryChangesW) , .

, ( ), :

"" , , ( ). , , - : ( IOException), , (, FileCopyCompleted).

+3

, :

  • FileSystemWatcher .
  • :

    . (. ), ( 1 ).

    . , , .

, , . , /, , , .

+2

. , reset . , .

+1

Changed - Renamed -event Changed -event, IOExceptions. , , Rename -event .

+1

FileSystemWatcher, -, , , - :

this.watcher.Created += (s, e) =>
{
    if (!this.seen.ContainsKey(e.FullPath) 
        || (DateTime.Now - this.seen[e.FullPath]) > this.seenInterval)
    {
        this.seen[e.FullPath] = DateTime.Now;
        ThreadPool.QueueUserWorkItem(
            this.WaitForCreatingProcessToCloseFileThenDoStuff, e.FullPath);
    }
};

this.seen - Dictionary<string, DateTime>, this.seenInterval - TimeSpan.

, ( ). , -, , , , FileNotFoundException, , , FileNotFoundException.

private void WaitForCreatingProcessToCloseFileThenDoStuff(object threadContext)
{
    // Make sure the just-found file is done being
    // written by repeatedly attempting to open it
    // for exclusive access.
    var path = (string)threadContext;
    DateTime started = DateTime.Now;
    DateTime lastLengthChange = DateTime.Now;
    long lastLength = 0;
    var noGrowthLimit = new TimeSpan(0, 5, 0);
    var notFoundLimit = new TimeSpan(0, 0, 1);

    for (int tries = 0;; ++tries)
    {
        try
        {
            using (var fileStream = new FileStream(
               path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                // Do Stuff
            }

            break;
        }
        catch (FileNotFoundException)
        {
            // Sometimes the file appears before it is there.
            if (DateTime.Now - started > notFoundLimit)
            {
                // Should be there by now
                break;
            }
        }
        catch (IOException ex)
        {
            // mask in severity, customer, and code
            var hr = (int)(ex.HResult & 0xA000FFFF);
            if (hr != 0x80000020 && hr != 0x80000021)
            {
                // not a share violation or a lock violation
                throw;
            }
        }

        try
        {
            var fi = new FileInfo(path);
            if (fi.Length > lastLength)
            {
                lastLength = fi.Length;
                lastLengthChange = DateTime.Now;
            }
        }
        catch (Exception ex)
        {
        }

        // still locked
        if (DateTime.Now - lastLengthChange > noGrowthLimit)
        {
            // 5 minutes, still locked, no growth.
            break;
        }

        Thread.Sleep(111);
    }

, , . 5- . , .

+1

myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
0
source

This answer is a bit late, but if possible, I get the original process for copying a small marker file after a large file or files and using FileWatcher.

0
source

All Articles