当另一个进程更改文本文件时,C#会收到通知

问题描述:

当另一个进程更改特定文本文件时,我希望在我的C#应用​​程序中收到通知。当另一个进程更改文本文件时,C#会收到通知

原因是我从我的应用程序启动第三方工具以检索有关设备的某些信息。此工具将设备的当前状态保存到ini文件中。这需要一些不确定的时间,但我想反应并尽快读取状态信息。

我该怎么做?

+1

这是基本的副本:http://*.com/questions/721714/notification-when-a-file-changes – arx 2012-01-14 23:17:55

您可以使用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"); 
} 

可以使用System.IO.FileSystemWatcher

而且监视文件的变化,看到Notification when a file changes?获取更多信息。