Sabitha
Sabitha

Reputation: 243

File System Watcher in C# gets fired only once

I am kicking off a File System Watcher in a system tray application. It monitor's a text file in a local folder. But the FS_changed event is fired only once after starting the application.

 FileSystemWatcher fWatch = new FileSystemWatcher();
 fWatch.BeginInit();     
 fWatch.Path = Path.GetTempPath();
 fWatch.Filter = "File1.txt";                       
 fWatch.Changed += new FileSystemEventHandler(fWatch_Changed);
 fWatch.EnableRaisingEvents = true;
 fWatch.EndInit();

I am not handling the Created/Deleted, kind of events. But I tried those as well(just to check) and they are not being called at all although that local file is being deleted and created during this process.

Any ideas/suggestions?

Upvotes: 2

Views: 1451

Answers (3)

Xtian Macedo
Xtian Macedo

Reputation: 835

Are you sure that the instance of fWatch is not being disposed? Is it part of a method that keeps listening all the time otherwise you might be getting the event only while the instance lives. Can you please put the code you are using to keep the watcher alive so we can help you more in detail.

Upvotes: 1

MethodMan
MethodMan

Reputation: 18843

as one of the overloads coud you pass the path that you're wanting to watch into the constructor of that object

 FileSystemWatcher fWatch = new FileSystemWatcher(Path.GetTempPath()); 

for example..?

Upvotes: 0

Jerod Venema
Jerod Venema

Reputation: 44632

It's probably getting garbage collected. Are you keeping a reference to that object?

Upvotes: 5

Related Questions