问题
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 :)
回答1:
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.
回答2:
Use FileSystemWatcher to be notified when your config file changes.
Use ConfigurationManager.RefreshSection to refresh it from disk without restarting your app.
回答3:
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.
回答4:
Create a FileSystemWatcher and listen to changes in the configuration file.
When triggered, use the following code:
MyGeneratedSettingsFile.Default.Reload();
回答5:
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.
回答6:
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.
回答7:
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
来源:https://stackoverflow.com/questions/468603/how-do-you-restart-an-app-reread-app-config-file-when-modified-net