views:

981

answers:

7

We all know that modifying a .NET web application's web.config file restarts the app and makes it read the updated configuration. How do you do that with a console or WinForms app? Thanks :)

+4  A: 

Probably need a FileSystemWatcher to monitor the file, and handle the relevant event. The System.Configuration.ConfigManager class might have the relevant methods to reload (method call or some such). I don't have VS in front of me, but I'd say there's definately some hooks there for sure.

DarkwingDuck
If you go down the FileSystemWatcher route, take care of actions that raise multiple events (not sure if it is still an issue in later revisions, but it was definitely still a gotcha for .NET 2.0)
ZombieSheep
it's the System.Configuration.ConfigurationManager class, with a RefreshSection() static method
cruizer
+1  A: 

There's a good section on actually doing the data reload here. Combine that with the FileSystemWatcher as suggested by DarkwingDuck, and you may well be onto a winner.

ZombieSheep
+1  A: 

Create a FileSystemWatcher and listen to changes in the configuration file.
When triggered, use the following code:

MyGeneratedSettingsFile.Default.Reload();
Patrik
A: 

log4net has the option of detecting external changes to *.config file. And as everyone else already has said this is done by using a FileSystemWatcher that is set to listen for changes to your *.config file.

Bottom line; FileSystemWatcher is the way to go.

Jesper Palm
A: 

There's a wider implication of simply reloading the configuration file though, and that is how the rest of the application should react to this.

For instance, you might configure up your database context layers for SQL Server after you've read from the configuration that you're going to talk to SQL Server and gotten the connection string for it.

However, what if the user changes that afterwards and specifies an Oracle database instead? It won't be enough to simply reload the configuration file, you need to be able to tell the rest of the application that something has changed as well.

I'd say you should abstract away the configuration part inside a new class, which has events. Once you decide to reload the configuration, this class should figure out internally which settings changed and fire the appropriate events.

If you're using an IoC container, you might need to re-wire some of its dependencies due to new settings.

This way, bits and pieces of your application that relies on configuration and might, for whatever reason, cache configuration settings locally for a given time interval would need to hook onto the relevant events and respond to them firing.

As for actually detecting and reloading the configuration file itself, the other answers seems to have covered that really good so I'm not going to repeat that here.

Lasse V. Karlsen
A: 

If you're using .NET Framework 2 and you're using user scope settings it's pretty transparent. After you change the settings just call the Save() method and the new settings are immediately available

For example

My.Settings.UpdateInterval = 10
My.Settings.Save()

If the setting is application scope then FileSystemWatcher might be the way to go

Conrad
+1  A: 

Use FileSystemWatcher to be notified when your config file changes.

Use ConfigurationManager.RefreshSection to refresh it from disk without restarting your app.

Juliet