I created a FileSystemWatcher object to watch for newly created files. Once a file is created, the FileCreated() method is called through the FileWatcher event handler. Problem is, after this happens, FileWatcher doesnt seem to listen anymore. I have to create a new FileWatcher every time the event is fired and the method completes. Below is my code. This is a seperate class from the Main class. From the Main, I call this class to set up and create the FileWatcher. I have tried to put the new FileSystemWatcher() in the MAIN class, but it stil quits listening after event. I also caled this watcher.WaitForChanged(WatcherChangeTypes.Created); after the FileCreated() method, but still nothing ... any help Thanks.
namespace
FileWatcherConsole{
public class DirectoryWatcher{
protected static FileSystemWatcher watcher = new FileSystemWatcher(); public static void XmlWatcher(){
watcher.Path = WatcherDir;
watcher.Filter =
"*.xml";watcher.NotifyFilter =
NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; watcher.Created += new FileSystemEventHandler(FileCreated);watcher.IncludeSubdirectories =
true;watcher.EnableRaisingEvents =
true;watcher.WaitForChanged(
WatcherChangeTypes.Created);}
}

FileSystemWatcher hell... stops watching after event
Eric Immerman
No, it is not a C# language question. It's a .NET framework question. There are many "dotnet" related forums and newsgroups you can peruse. Check out http://msdn.microsoft.com/newsgroups, and go to .NET Development.
Antoine Sirianni
I removed the WaitForChanged, and added a Console.ReadLine();
Same thing is happening. It fires the even for the first fire, then just seems to stop listening. My console app is still open and running, it just doesnt seem to do anything beyond the first file creation. Here is my code with the Main. Thanks for the help.
class
FileWatcherMain{
/// <summary> /// The main entry point for the application. /// </summary>[
STAThread] static void Main(string[] args){
DirectoryWatcher who = new DirectoryWatcher();who.XmlWatcher();
string hell = Console.ReadLine();}
}
public class DirectoryWatcher
{
FileSystemWatcher watcher = null; // Defines the directories for the DirectoryWatcher Object protected static string WatcherDir = ConfigurationManager.AppSettings"DirectoryWatcherCurrentDir"].ToString(); public void XmlWatcher(){
this.watcher = new FileSystemWatcher(); watcher.Path = WatcherDir;watcher.Filter =
"*.xml";watcher.NotifyFilter =
NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;watcher.Created += new FileSystemEventHandler(FileCreated);watcher.IncludeSubdirectories =
true;watcher.EnableRaisingEvents =
true;}
public void FileCreated(object source, FileSystemEventArgs e){
Console.WriteLine("HEY IT WORKS!!!****************"); string hef = Console.ReadLine();}
blumash
So what's happening in your case I think is that when you call WaitForChanged from your Created event handler you block the file system watcher thread, which is not the same as your main application thread, so that it cannot handle any more file creations.
Ernestoguillen
DanTakacs
anna-jayne
The console streams are synchronized, so when your main thread call Console.ReadLine it will take the lock for the standard input stream and the file system watcher thread will then wait on its call to ReadLine. But since your console application will exit when you press enter and cause the first Console.ReadLine call to return you'll only ever see one file creation.
Internationalpaddy
fums64
The method that is fired upon the creation of a file is FileCreated().
The last thing this method does is : watcher.WaitForChanged(WatcherChangeTypes.Created);
The console app remains running. It just does not respond to the creation of subsequent files.