FileSystemWatcher with the console application
Asked Answered
A

1

9

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);
Allottee answered 29/11, 2016 at 6:28 Comment(4)
Have you made your program wait in the MAIN method after started watching?Protecting
I am asking the doubt here what will be the correct method to be watched. Yes I have made to wait in the main method.Allottee
Your code seems to be correct but you should be aware of that it only watches the files are in the root , not the file are in the in sub-folders.Protecting
Thanks. I will check that and add that condition. Here I have two questions. So I need to add the code [new System.Threading.AutoResetEvent(false).WaitOne(); ] to be watched always.. I want to know the reason that why we should watch only the rootfolder not the subfolder?.Allottee
P
20

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.

Protecting answered 29/11, 2016 at 9:40 Comment(2)
I am going to check only xml file which is only added to the specific folder (This will occur occasionally) and I have used NotifyFilter to know only for the filename and I am going to check only for the root folder. So will this yield buffer overflow.Allottee
@JeevaJsb How often does the file change? if it's so frequent then you've got to increase the InternalBufferSize as I mentioned above.Protecting

© 2022 - 2024 — McMap. All rights reserved.