当另一个进程更改文本文件时,C#会收到通知
问题描述:
当另一个进程更改特定文本文件时,我希望在我的C#应用程序中收到通知。当另一个进程更改文本文件时,C#会收到通知
原因是我从我的应用程序启动第三方工具以检索有关设备的某些信息。此工具将设备的当前状态保存到ini文件中。这需要一些不确定的时间,但我想反应并尽快读取状态信息。
我该怎么做?
答
您可以使用System.IO.FileSystemWatcher类。 事情是这样的:
string fileToWatch = @"C:\MyFile.txt";
fileSystemWatcher = new FileSystemWatcher(fileToWatch);
void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
Debug.WriteLine(e.Name + " has changed");
}
这是基本的副本:http://*.com/questions/721714/notification-when-a-file-changes – arx 2012-01-14 23:17:55