How to determine if copying of a folder has finished?

In a project, I have a working folder. If we copy files or file folders from some folder to this folder, I need to determine if the copy was completed or not. After that, I will process the files in this working folder.

FileSystemWatcher theWatcher = new FileSystemWatcher(workingFolder);
theWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
                          NotifyFilters.FileName | NotifyFilters.DirectoryName |
                          NotifyFilters.Size;
theWatcher.IncludeSubdirectories = true;
theWatcher.Created += OnFolderChanged;
theWatcher.EnableRaisingEvents = true;

      

Whenever the file is copied, the OnFolderChanged event will be fired. So at first I tried to get the eclipse time between two copies of the files, but the eclipse time is very difficult to determine due to network speed and file size. I also tried the method in Find if the copy process is in the Directory but the token file may still be open when copying is complete.

Any good way to handle this?

+3


source to share


2 answers


You cannot reverse engineer the process of a process that you do not know anything about the FileSystemWatcher. There are many possible failure modes. A good example would be a user using Explorer to copy files and displaying a dialog since the destination file already exists. When the user has disabled the bathroom break as they counted on, it takes a while.



You should never make any assumptions about what is going on. Randomly start the timer with 1 minute when you receive a notification and restart it when you receive another. When the timer goes off, you have reason to believe that the copy operation is complete. Just make sure it's never a problem when you get it wrong.

+2


source


If you have control over the process that is copying the file / folder, you can add a suffix (.complete) to the file / folder when it finishes copying and configures the file system watcher to only respond to files / folders with the .complete extension.

Update. Since you have no control over the names of the copied files / folders, I would use exception handling in the method FolderChaged

. I am assuming that you are doing some processing on the file / folder in this method and that if you try to access a file / folder that has not finished copying, you will get some kind of exception System.IO

.



I ran into this problem with FSW as well, and I dealt with it by catching the exception, pausing for a few seconds, and then trying to access the file again. I put this logic in a loop that will retry accessing the file 5 times within 10 seconds. If we still cannot process the file after 5 tries, we throw an exception and our standard error handling takes over.

Of course, it is still an open question as to what are the right loop parameters for you and, if you have reached the limit of those parameters, how do you deal with the failure?

0


source







All Articles