3

I would like to be notified in my C# application when another process makes changes to a particular textfile.

The reason for this is that I launch a 3rd party tool from my application in order to retrieve some information about a device. this tool saves the current state of the device into an ini file. This takes some undetermined time, but I want to react and read the state information as soon as it's available.

How can I do this?

Mat
  • 4,281
  • 9
  • 44
  • 66
  • 1
    This is basically a duplicate of: http://stackoverflow.com/questions/721714/notification-when-a-file-changes – arx Jan 14 '12 at 23:17

2 Answers2

3

You could use the System.IO.FileSystemWatcher class. Something like this:

string fileToWatch = @"C:\MyFile.txt";
fileSystemWatcher = new FileSystemWatcher(fileToWatch);

void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
    Debug.WriteLine(e.Name +  " has changed");
}
Todd Bellamy
  • 152
  • 3
2

You can monitor file changes using System.IO.FileSystemWatcher

Also, see Notification when a file changes? for more info.

Community
  • 1
  • 1
Arnold Zokas
  • 8,306
  • 6
  • 50
  • 76