For example, there is a program with some app.config where the path for downloading files is stored.

Can the program understand when it is running that the config has been edited and based on this, download to another folder?

  • if the data in the test is different, then the config has changed and you do everything. what you want. - Senior Pomidor
  • @SeniorAutomator, this is understandable. It turns out that you need to check the configuration on the timer or can you do something else? - iluxa1810
  • 2
    Use the ReadDirectoryChanges API function or its equivalent in .Net. - ߊߚߤߘ
  • @ iluxa1810 yes. or in a separate thread to read. either ReadDirectoryChanges to use, as said above - Senior Pomidor

1 answer 1

Yes maybe.

First you need to explicitly upload your settings file:

 var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 

Next you need to get a list of files from which this settings file was compiled:

 var files = config.Locations.Cast<ConfigurationLocation>().Select(loc => loc.Path); 

Now you can start monitoring these files:

 var monitor = new HostFileChangeMonitor(files.ToArray()); monitor.NotifyOnChanged(_ => { // ... }); 

When the signal about changing files arrives, you must wait 100-200 milliseconds and do everything from the beginning.

  • How is HostFileChangeMonitor better than traditional FileSystemWatcher ? - VladD
  • @VladD is just much easier to use. When using FileSystemWatcher, you need to group files by directories - and HostFileChangeMonitor does it internally. Again, when using FileSystemWatcher it is very easy to be tempted to monitor a permanent list of files - and in fact it is variable (see the configSource attribute)! HostFileChangeMonitor does not allow such an error because of its “one-timeness”. - Pavel Mayorov
  • Thank you, did not know. - VladD