gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:59:47 24 4
gpt4 key购买 nike

我刚刚将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com