Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.FileSystemWatcher does not watch file changed by Visual Studio 2013

I just upgraded Visual Studio to version 2013 Ultimate, I found System.IO.FileSystemWatcher class failed to watch file edited by Visual Studio 2013. suppose I have below code

class Program
{
    static void Main(string[] args)
    {
        var watcher = new FileSystemWatcher(@"C:\test", "*.txt");
        watcher.Changed += watcher_Changed;
        watcher.EnableRaisingEvents = true;
        Console.Read();
        watcher.Changed -= watcher_Changed;
    }

    static void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("file is changed");
    }
}

If I edit file C:\test\a.txt with notepad, the program will report the file is change, but if I edit it with Visual Studio 2013, my program keep silent. Why?

like image 274
Shuping Avatar asked Nov 11 '13 11:11

Shuping


1 Answers

I noticed that when editing files in Visual Studio 2013, it created temporary files then deletes the original file and renames the temporary file to the same name. So to catch normal edits handle the System.IO.FileSystemWatcher's Changed event, and for edits in Visual Studio, handle the Renamed event.

like image 139
Marcus L Avatar answered Oct 16 '22 09:10

Marcus L