I am trying to use the FileSystemWatcher – and am having some luck..
The goal is to MOVE the file that gets created, from the monitored folder, to a new folder.
But… have hit 2 snags. Firstly, if I move 3 files into a folder at once (Select 3 files, ctrl+x, and then ctrl+c into my Monitor Folder), the monitor only triggers for the first file. The other 2 don’t get processed.
FileSystemWatcher fsw = new FileSystemWatcher(FolderToMonitor);
fsw.Created += new FileSystemEventHandler(fsw_Created);
bool monitor = true;
while (monitor)
{
fsw.WaitForChanged(WatcherChangeTypes.All, 2000);
if (Console.KeyAvailable)
{
monitor = false;
}
}
Show("User has quit the process...", ConsoleColor.Yellow);
Console.ReadKey();
Is there a way to make it see all 3?
Secondly, if I move a file into the monitor folder, from a different drive, it takes a few seconds to copy the file into the folder. However, the monitor triggers as soon as the file starts copying in.. so therefore, is read only, and not ready to be moved.
Is there a way I can wait for the file to complete it’s copy into the monitor folder, before I process it?
According to the msdn documentation:
Perhaps that explains your issue?
Also note that cutting and pasting files from one directory to another is basically a mere renaming operation, therefore you should use the
Renamedevent to detect them.As for your other problem: try using the
Changedevent together withCreated, as I believe both will be raised exactly once for a file (note that moving a file from another drive in not a simple renaming operation: it’s copy and delete), so theChangedevent should indicate when the file copy operation has been completed (i.e. it won’t fire until the file is complete).