Question

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?

Was it helpful?

Solution

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");
}

OTHER TIPS

You can monitor file changes using System.IO.FileSystemWatcher

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top