In a project I have a working folder. If we copy files or folders of files from somewhere to this folder, I need to detect if the copying has been finished or not. After that I'd handle 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 a file is copying, OnFolderChanged event will be triggered. So at first I tried to get the eclipse time between 2 file copying, but the eclipse time is very hard to determine due to network speed and file size. I also tried the method in Find if a Copy process is in progress on Directory, but the token file still can be opened when copying is in process.
Any good way to handle this?
You cannot possibly reverse-engineer the operation of a process you know nothing about with FileSystemWatcher. There are many possible failure modes. A good example would be the user using Explorer to copy the files and it showing a dialog because the destination file already exists. With the user off taking a bathroom break since he counted on it taking a while.
You should never make any assumptions about what is going on. Arbitrarily, start a one minute timer when you get a notification and restart it when you get another one. When the timer ticks then you have some reason to guess that the copy operation is complete. Just make sure that it is never a problem when you guessed wrong.