Search code examples
c#text-filesinter-process-communicat

C# get notified when another process makes changes to a textfile


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?


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