简体   繁体   中英

How to dynamically apply changes to app.config on program start?

My program uses some environment data, which is referenced in 'app.config' file:

<configuration>
...
   <EnvUnderTest path="settings_a.xml" />
...
<configuration>

It's needed to switch between this data from times to times. But, I don't like an idea to change 'app.config' file directly. So, I thought that it'll be much better if 'path' parameter would be changed right after program start.

Could you tell me, how it could be done?

I've tried the following code:

string te = ConfigurationManager.AppSettings["EnvUnderTest "]; 
Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

but te variable value is null , so I have no idea how could I access this parameter value on runtime.

You're not using the Config file appropriately. Try this:

<configuration>
...
    <appSettings>

        <add key="EnvUnderTest" value="settings_a.xml" />
    </appSettings>
...
</configuration>

This should work with the code you've provided. Obviously, any other key you wish to add should also be under <appSettings> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM