FileSystemWatcher with the console application

Jeeva J picture Jeeva J · Nov 29, 2016 · Viewed 11.3k times · Source

To send bulk email in my web application I am using filewatcher to send the application.

I have planned to write filewatcher with the console application instead of windows service or scheduler.

I have copied the executable file shortcut in the following path.

%appdata%\Microsoft\Windows\Start Menu\Programs

Ref: https://superuser.com/questions/948088/how-to-add-exe-to-start-menu-in-windows-10

After run the executable file the file watcher is not watched always. After searching some sites, i have found that we need to add the code

new System.Threading.AutoResetEvent(false).WaitOne();

Is this the right method to add in the executable file and to watch the folder?

After run the console application (without above code) is the file won't be watched always?

What will be the right method to use the file watcher?

FileSystemWatcher watcher = new FileSystemWatcher();
string filePath = ConfigurationManager.AppSettings["documentPath"];
watcher.Path = filePath;
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.FileName; 
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(OnChanged);

Answer

Ali Bahrami picture Ali Bahrami · Nov 29, 2016

Since it's a Console Application you need to write a code in the Main method to wait and not to close immediately after running codes.

static void Main()
{
   FileSystemWatcher watcher = new FileSystemWatcher();
   string filePath = ConfigurationManager.AppSettings["documentPath"];
   watcher.Path = filePath;
   watcher.EnableRaisingEvents = true;
   watcher.NotifyFilter = NotifyFilters.FileName;
   watcher.Filter = "*.*";
   watcher.Created += new FileSystemEventHandler(OnChanged);


   // wait - not to end
   new System.Threading.AutoResetEvent(false).WaitOne();
}

Your code only tracks changes in the root folder, if you wanted to watch the subfolders you need to set IncludeSubdirectories=true for your watcher object.

static void Main(string[] args)
{
   FileSystemWatcher watcher = new FileSystemWatcher();
   string filePath = @"d:\watchDir";
   watcher.Path = filePath;
   watcher.EnableRaisingEvents = true;
   watcher.NotifyFilter = NotifyFilters.FileName;
   watcher.Filter = "*.*";

   // will track changes in sub-folders as well
   watcher.IncludeSubdirectories = true;

   watcher.Created += new FileSystemEventHandler(OnChanged);

   new System.Threading.AutoResetEvent(false).WaitOne();
}

You must also be aware of buffer overflow. FROM MSDN FileSystemWatcher

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to losing track of changes in the directory, and it will only provide the blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.