2012-01-14 87 views
3

當另一個進程更改特定文本文件時,我希望在我的C#應用​​程序中收到通知。當另一個進程更改文本文件時,C#會收到通知

原因是我從我的應用程序啓動第三方工具以檢索有關設備的某些信息。此工具將設備的當前狀態保存到ini文件中。這需要一些不確定的時間,但我想反應並儘快讀取狀態信息。

我該怎麼做?

+1

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

回答

3

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