Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher with the console application

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);
like image 300
Jeeva J Avatar asked Nov 29 '16 06:11

Jeeva J


People also ask

What is FileSystemWatcher in C#?

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

What is a FileSystemWatcher?

The FileSystemWatcher class in the System.IO namespace can be used to monitor changes to the file system. It watches a file or a directory in your system for changes and triggers events when changes occur. In order for the FileSystemWatcher to work, you should specify a directory that needs to be monitored.

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

The FileSystemWatcher provides us with the event handlers to capture events like renamed, deleted, created and changed.

Is FileSystemWatcher multithreaded?

Nope, filesystemwatchers run on their own thread.


1 Answers

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.

like image 199
Ali Bahrami Avatar answered Oct 01 '22 00:10

Ali Bahrami