c# - System.IO.FileSystemWatcher 不监视 Visual Studio 2013 更改的文件

标签 c# visual-studio-2013

我刚刚将 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.FileSystemWatcherChanged 事件,对于 Visual Studio 中的编辑,请处理 Renamed 事件。

关于c# - System.IO.FileSystemWatcher 不监视 Visual Studio 2013 更改的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19905151/

相关文章:

c++ - 当我用 VS2013 编译时链接到用 VS2010 编译的静态 .lib?

c++ - STL const_iterator 转换——编译器差异

c# - 这个 ConcurrentDictionary 线程安全吗?

c# - C# 中是否可能进行部分泛型类型推断?

c# - 如何调用WebService客户端?

c# - 抑制处理器不匹配警告

javascript - 文件 uploader 调用函数

c# - .NET 中是否有任何内置的稳定排序例程和交换函数?

c++ - 你如何告诉 Visual Studio 编译外部库?

c# - 为什么 Convert.ChangeType() 不将日期字符串(以 dd/MM/yyyy 格式)转换为日期时间类型?