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 :)
我们都知道修改.NET Web应用程序的web.config文件会重新启动应用程序并使其读取更新的配置。你是如何使用控制台或WinForms应用程序做到这一点的?谢谢 :)
7 个解决方案
#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.
可能需要FileSystemWatcher来监视文件,并处理相关事件。 System.Configuration.ConfigManager类可能具有重新加载的相关方法(方法调用或类似方法)。我没有在我面前的VS,但是我肯定肯定会有一些钩子。
#2
Use FileSystemWatcher to be notified when your config file changes.
使用FileSystemWatcher在配置文件更改时收到通知。
Use ConfigurationManager.RefreshSection to refresh it from disk without restarting your app.
使用ConfigurationManager.RefreshSection从磁盘刷新它而不重新启动应用程序。
#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.
这里有一个关于实际进行数据重新加载的好部分。根据DarkwingDuck的建议将其与FileSystemWatcher结合使用,你可能会成为胜利者。
#4
Create a FileSystemWatcher and listen to changes in the configuration file.
When triggered, use the following code:
创建FileSystemWatcher并侦听配置文件中的更改。触发后,使用以下代码:
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.
log4net可以选择检测* .config文件的外部更改。正如其他人已经说过的,这是通过使用FileSystemWatcher完成的,该文件设置为侦听* .config文件的更改。
Bottom line; FileSystemWatcher is the way to go.
底线; FileSystemWatcher是要走的路。
#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.
例如,您可以在从要与SQL Server通信的配置中读取并为其获取连接字符串后,为SQL Server配置数据库上下文层。
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.
但是,如果用户之后更改并指定Oracle数据库,该怎么办?仅仅重新加载配置文件是不够的,您需要能够告诉应用程序的其余部分也发生了一些变化。
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.
如果您正在使用IoC容器,则可能需要重新连接其某些依赖项,因为新设置。
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
如果您使用的是.NET Framework 2并且您正在使用用户范围设置,则它非常透明。更改设置后,只需调用Save()方法,即可立即使用新设置
For example
My.Settings.UpdateInterval = 10
My.Settings.Save()
If the setting is application scope then FileSystemWatcher might be the way to go
如果设置是应用程序范围,则可能是FileSystemWatcher
#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.
可能需要FileSystemWatcher来监视文件,并处理相关事件。 System.Configuration.ConfigManager类可能具有重新加载的相关方法(方法调用或类似方法)。我没有在我面前的VS,但是我肯定肯定会有一些钩子。
#2
Use FileSystemWatcher to be notified when your config file changes.
使用FileSystemWatcher在配置文件更改时收到通知。
Use ConfigurationManager.RefreshSection to refresh it from disk without restarting your app.
使用ConfigurationManager.RefreshSection从磁盘刷新它而不重新启动应用程序。
#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.
这里有一个关于实际进行数据重新加载的好部分。根据DarkwingDuck的建议将其与FileSystemWatcher结合使用,你可能会成为胜利者。
#4
Create a FileSystemWatcher and listen to changes in the configuration file.
When triggered, use the following code:
创建FileSystemWatcher并侦听配置文件中的更改。触发后,使用以下代码:
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.
log4net可以选择检测* .config文件的外部更改。正如其他人已经说过的,这是通过使用FileSystemWatcher完成的,该文件设置为侦听* .config文件的更改。
Bottom line; FileSystemWatcher is the way to go.
底线; FileSystemWatcher是要走的路。
#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.
例如,您可以在从要与SQL Server通信的配置中读取并为其获取连接字符串后,为SQL Server配置数据库上下文层。
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.
但是,如果用户之后更改并指定Oracle数据库,该怎么办?仅仅重新加载配置文件是不够的,您需要能够告诉应用程序的其余部分也发生了一些变化。
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.
如果您正在使用IoC容器,则可能需要重新连接其某些依赖项,因为新设置。
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
如果您使用的是.NET Framework 2并且您正在使用用户范围设置,则它非常透明。更改设置后,只需调用Save()方法,即可立即使用新设置
For example
My.Settings.UpdateInterval = 10
My.Settings.Save()
If the setting is application scope then FileSystemWatcher might be the way to go
如果设置是应用程序范围,则可能是FileSystemWatcher