Common app.config between applications

I need a common configuration file shared by three applications

I solved this by adding to the file in appSettings

<appSettings file="ait.config"> <!--<add key="Culture" value="zh-CN" />--> <add key="Culture" value=""/> <add key="ClientSettingsProvider.ServiceUri" value=""/> </appSettings> 

Ait.config stores some common values, such as

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

If I try to read it as

  string stvalue = ConfigurationManager.AppSettings["Username"]; 

It works fine, but if I try to write a value like

 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["Username"].Value = userName; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 

Instead of writing to a common ait.config file, add the key username to the standard app.config in each individual application, does anyone know how to solve this.

+6
source share
3 answers

Using your method, you are changing the current configuration, not the file you want, because this is the document you opened. Unfortunately, the general config does not seem to be โ€œacceptedโ€ as a configuration file, since it lacks the node configuration. You can open the general configuration as a โ€œregularโ€ xml document from the configuration (after line of code 1):

 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(config.AppSettings.File); 

Then you just have to change the document. Shame on me is not good in linq for xml, it works below, but there is probably an easier way.

 XmlNode n = xmlDoc.SelectSingleNode("//appSettings"); XmlNode x = n.ChildNodes[0]; x.Attributes[1].Value = userName; xmlDoc.Save(config.AppSettings.File); 
+1
source

I believe that the path to the default application settings files does not match app.config. can be refreshing, it can help. havent checked this, but please try this - add this line of code after initializing the configuration and before calling save

 config.AppSettings.File = config.FilePath; 

NTN

0
source

The standard approach for separating settings for individual files is to use the ConfigSource ConfigurationSection . Therefore, if you want to save ALL of your AppSettings in "ait.config", you can do like this:

 <appSettings configSource="ait.config" /> 

Please note that this only works if you save ALL of your AppSettings in a separate file. If you try to save some parameters in your main application configuration file, for example:

 <appSettings configSource="ait.config"> <add key="Culture" value=""/> <add key="ClientSettingsProvider.ServiceUri" value=""/> </appSettings> 

You get an exception saying something like " Sections should only appear once in the configuration file. ".

You should probably use the Customizing section for general configurations, try:

Here is a simple example to get you started:

Define your section:

 public class CommonConfSection : ConfigurationSection { private const String UserNameProperty = "Username"; [ConfigurationProperty(UserNameProperty, DefaultValue = "__UNKNOWN_NAME__", IsRequired = false)] public String UserName { get { return (String)this[UserNameProperty]; } set { this[UserNameProperty] = value; } } } 

App.config:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="CommonSettings" type="[Your Assembly Name].CommonConfSection, [Your Assembly Name]" /> </configSections> <appSettings> <add key="Culture" value=""/> <add key="ClientSettingsProvider.ServiceUri" value=""/> </appSettings> <CommonSettings configSource="common.config" /> </configuration> 

Note:

  • configSections MUST be the first child of a configuration node ;
  • The CommonSettings : configSource = "common.config" section says that the settings for this section are stored in a file called " common.config >" (in the same folder).

And here is what common.config looks like :

 <?xml version="1.0" encoding="utf-8" ?> <CommonSettings Username="testUser" /> 
0
source

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


All Articles