Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FileSystemWatcher to monitor a directory

I am using a Windows Forms Application to monitor a directory and move the files dropped in it to another directory.

At the moment it will copy the file to another directory, but when another file is added it will just end with no error message. Sometimes it does copy two files before ending on the third.

Is this because I am using a Windows Form Application rather than a console app? Is there a way I can stop the program from ending and to keep watching the directory?

private void watch() {   this.watcher = new FileSystemWatcher();   watcher.Path = path;   watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite                          | NotifyFilters.FileName | NotifyFilters.DirectoryName;   watcher.Filter = "*.*";   watcher.Changed += OnChanged;   watcher.EnableRaisingEvents = true; }  private void OnChanged(object source, FileSystemEventArgs e) {   //Copies file to another directory. }  public void Dispose() {   // avoiding resource leak   watcher.Changed -= OnChanged;   this.watcher.Dispose(); } 
like image 581
cheeseman Avatar asked Feb 22 '13 05:02

cheeseman


People also ask

How do I monitor a folder to a new file?

To monitor a folder for new files in Windows with PowerShell, we can use a . NET class called FileSystemWatcher. This class is in the System.IO namespace and can be created with the New-Object cmdlet.

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.


2 Answers

The problem was the notify filters. The program was trying to open a file that was still copying. I removed all of the notify filters except for LastWrite.

private void watch() {   FileSystemWatcher watcher = new FileSystemWatcher();   watcher.Path = path;   watcher.NotifyFilter = NotifyFilters.LastWrite;   watcher.Filter = "*.*";   watcher.Changed += new FileSystemEventHandler(OnChanged);   watcher.EnableRaisingEvents = true; } 
like image 99
cheeseman Avatar answered Sep 24 '22 13:09

cheeseman


You did not supply the file handling code, but I assume you made the same mistake everyone does when first writing such a thing: the filewatcher event will be raised as soon as the file is created. However, it will take some time for the file to be finished. Take a file size of 1 GB for example. The file may be created by another program (Explorer.exe copying it from somewhere) but it will take minutes to finish that process. The event is raised at creation time and you need to wait for the file to be ready to be copied.

You can wait for a file to be ready by using this function in a loop.

like image 29
nvoigt Avatar answered Sep 25 '22 13:09

nvoigt