3
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = @"C:\foo.txt";
fsw.Changed += new FileSystemEventHandler(LogFileChanged);

private void LogFileChanged(object s, FileSystemEventArgs e)
{ 

}

If i put a breakpoint in LogFileChanged() -> open and edit foo.txt -> saves the file, the breakpoint doesn't hit. Can someone explain what I missed?

Johan
  • 35,120
  • 54
  • 178
  • 293
  • where is the breakpoint? – Davin Tryon Dec 14 '12 at 23:34
  • @dtryon in the `LogFileChanged` method – Johan Dec 14 '12 at 23:34
  • Have you tried to add `System.Diagnostics.Debugger.Launch();` instead of the breakpoint? Might at least prove that it is not an attachment issue. – Davin Tryon Dec 14 '12 at 23:36
  • 1
    A good text editor never changes a file, that can cause irretrievable data loss. Instead it writes a new file, renames the old file, renames the new file, deletes the old file. No change event. – Hans Passant Dec 14 '12 at 23:41
  • BTW, your `LogFileChanged()` method shouldn't `static` ? Because you can't access it from `FileSystemEventHandler` without defining its' class object. – Soner Gönül Dec 14 '12 at 23:41
  • Are you 100% sure that there is change event? Consider writing test program that actually explicitly open -> save -> close (instead of new -> save -> delete old/rename new sequence). – Alexei Levenkov Dec 14 '12 at 23:47

2 Answers2

8

This is a file not a path

fsw.Path = @"C:\foo.txt";

you need to set

fsw.Path = @"C:\";
fsw.Filter = "foo.txt";

Filter Property

Steve
  • 213,761
  • 22
  • 232
  • 286
2

Have you set this property?

 fsw.EnableRaisingEvents = true;
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445