someone
someone

Reputation: 29

FileSystemWatcher not always firing

FileSystemWatcher works if I change the file with notepad.exe, but not if I change the file with VisualStudio. Why? See also: Powershell File Watcher Not Picking Up File Changes Made in Visual Studio

static void FileWatcher()
{
    FileSystemWatcher watcher = new FileSystemWatcher
    {
        Path = Path.GetDirectoryName(@"D:\Test\"),
        NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
        Filter = "file.txt",
        EnableRaisingEvents = true
    };
    watcher.Changed += OnFileChanged;
}

static void OnFileChanged(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("{0}  Watcher:  {1}  {2}", DateTime.Now, e.ChangeType, e.FullPath);
}

PS. watcher.Renamed works. Thank you mjwills.

Upvotes: 0

Views: 93

Answers (1)

Artyom Ignatovich
Artyom Ignatovich

Reputation: 601

By adding the following lines of code you should be able to capture all events.

watcher.Deleted += OnFileChanged;
watcher.Created += OnFileChanged;

Upvotes: 1

Related Questions