Write appSettings to an external file

I have a configuration file. app.exe.config and appSettings have something like this:

<configuration> <appSettings configSource="app.file.config" /> </configuration> 

The app.file.config file has something like this:

 <?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="var1" value="value 1" /> <add key="var2" value="value 2" /> <add key="var3" value="value 3" /> </appSettings> 

I need to change var1, var2 and var3 at runtime, and I have code like this:

 Configuration config = ConfigurationManager.OpenExeConfiguration("...path\app.exe); config.AppSettings.SectionInformation.ConfigSource = "app.file.config"; config.AppSettings.Settings["var1"].Value = "value 11"; config.AppSettings.Settings["var2"].Value = "value 22"; config.AppSettings.Settings["var3"].Value = "value 33"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 

When I run config.Save ..., the app.file.config file has an appSettings node with the file attribute. This attribute has the value app.file.config

 <appSettings file="app.file.config"> <add key="var1" value="value 1" /> <add key="var2" value="value 2" /> <add key="var3" value="value 3" /> </appSettings> 

Now, if I try to load the configuration file, I have an exception with the message "Unrecognized attribute" file. Note that attribute names are case sensitive. "In the app.file.config file.

If I manually delete the file attribute, the configuration file will load properly.

Any ideas?

Avoid writing file attribute while saving configuration files.

thanks

+6
source share
3 answers

Finally, I found a solution.

The solution is to declare the configuration file as follows:

 <appSettings configSource="app.file.config"> <add key="var1" value="value 1" /> <add key="var2" value="value 2" /> <add key="var3" value="value 3" /> </appSettings> 

And from the code

 Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); AppSettingsSection myAppSettings = config.GetSection("appSettings") myAppSettings.Settings["var1"].Value = "value 11"; config.Save(ConfigurationSaveMode.Modified); 

Please note that I use GetSection ("AppSettings") instead of config.AppSettings.Settings

Thanks to everyone who helps people at StackOverflow.

-2
source

using an external configuration file is transparent to the application,

this part is ok

 </configuration> <appSettings configSource="app.file.config" /> </configuration> 

as well as this:

 <?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="var1" value="value 1" /> <add key="var2" value="value 2" /> <add key="var3" value="value 3" /> </appSettings> 

change your code like this:

 Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); config.AppSettings.Settings["var1"].Value = "value 11"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 

accessing an external configuration file is transparent to the application, so you do not need to call it directly. you can use the default appSetting section in configuration manager.

Good luck.

+11
source

WARNING : configSource is not the correct answer as indicated in other answers.

Now for the bad news ... none of the external files will be saved if you update the value read using the ConfigurationManager.

Setup: Command Line Project App.config File:

 <appSettings file="AppSettings.config"> <add key="test" value="MAIN" /> </appSettings> 

AppSettings.config file with "Copy to output directory" = "Always copy"

 <?xml version="1.0" encoding="utf-8"?> <appSettings> <add key="test" value="OVERRIDDEN"/> </appSettings> 

Program.cs:

  Console.WriteLine("Local Config sections"); try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("press the ENTER key to end"); Console.ReadLine(); 

TEST1 - appsettings settings overridden by configSource sub-config

Sort:

Change app.config to use 'configSource'

 <appSettings configSource="AppSettings.config"> 

Act: RunProject

Assert: Exception Raised: "Partitions should appear only once in the configuration file."

TEST2 - appsettings settings overridden by configSource sub-config

Sort:

Change app.config to use 'file'

 <appSettings file="AppSettings.config"> 

Act: RunProject

Approve:

config.AppSettings.Settings ["test"]. Value = OVERRIDDEN

config.AppSettings.Settings ["testExternalOnly"]. Value = INITIALSTATE

Now for the bad news ... none of the external files will be saved if you use

 config.AppSettings.Settings["test"].Value = "NEW"; 

AppSettings will only be saved in the .exe.config file, and then, if you close and open your configuration, the values ​​will be overwritten by the external values ​​of the file.

TEST3 . Values ​​in external files are not updated in the "config.Save" section

Setting: Program.cs changed to:

  Console.WriteLine("Local Config sections"); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("BEFORE[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("BEFORE[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); config.AppSettings.Settings["test"].Value = "NEW"; config.AppSettings.Settings["testExternalOnly"].Value = "NEWEXTERNAL"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); //Shut current config config = null; //Open config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("AFTER[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("AFTER[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); Console.WriteLine("AFTER CONFIG EXTERNAL FILE: " + System.IO.File.ReadAllText("AppSettings.config")); Console.WriteLine("AFTER CONFIG FILE: " + System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.FriendlyName + ".config")); 

Act: Run the program

Approve:

{myExe} .exe.confg AppSettings section:

 <appSettings file="AppSettings.config"> <add key="test" value="NEW" /> <add key="testExternalOnly" value="NEWEXTERNAL" /> </appSettings> 

AppSettings.config does not change

0
source

Source: https://habr.com/ru/post/980261/


All Articles