我刚刚将 Visual Studio 升级到 2013 Ultimate 版本,我发现 System.IO.FileSystemWatcher
类无法监视 Visual Studio 2013 编辑的文件。假设我有以下代码
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");
}
}
如果我用记事本编辑文件C:\test\a.txt
,程序会报告文件有变化,但如果我用Visual Studio 2013编辑它,我的程序会保持沉默。为什么?
最佳答案
我注意到在 Visual Studio 2013 中编辑文件时,它会创建临时文件,然后删除原始文件并将临时文件重命名为相同的名称。因此,要捕获正常编辑,请处理 System.IO.FileSystemWatcher
的 Changed
事件,对于 Visual Studio 中的编辑,请处理 Renamed
事件。
关于c# - System.IO.FileSystemWatcher 不监视 Visual Studio 2013 更改的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19905151/