klashagelqvist
klashagelqvist

Reputation: 1261

Common app.config between applications

I have a need for a common config file shared by three applications

I solved it by adding in a file in appSettings

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

In the ait.config i store some common values like

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

If i try to read it like

 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 the common file ait.config it add the key username to the standard app.config in each seperate application, anyone knows how to solve this.

Upvotes: 6

Views: 1704

Answers (3)

Jiaji Wu
Jiaji Wu

Reputation: 469

The standard approach to divide settings to separate files is to use the ConfigSource of ConfigurationSection. Therefore, if you want to keep ALL of your AppSettings in "ait.config", you could do like this:

<appSettings configSource="ait.config" />

Note, however, this only works if you keep ALL your AppSettings in a separate file. If you try to keep some of the settings in your main app config file like:

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

You get an exception saying something like "Sections must only appear once per config file.".

You should probably use a "Custom Configuration Section" for the shared configurations, try:

  1. Create Custom Configuration Sections Using ConfigurationSection.
  2. Point the ConfigSource of your Custom Configuration Section to the file containing common configs.

Here is a simple example to get started:

Define your custom 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:

  1. configSections MUST be the first child node of configuration;
  2. The CommonSettings section: configSource="common.config" says the settings for this section is stored in the file named "common.config" (in the same folder).

And here is what common.config looks like:

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

Upvotes: 0

AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

With your method, you change the current config, not the file you want, because this is the document you opened. Unfortunately, the shared config seems not to be "accepted" as config file because it lacks the configuration node. You can open the shared config as a "normal" xml document from config (after your code line 1):

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

Then you should just change the doc. Shame on me I am not good in linq to xml, the below works, 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);

Upvotes: 1

Krishna
Krishna

Reputation: 2481

I believe the app settings files path is defaulting to app.config. may be refreshing it may help. havent tested this, but please try this - add this line of code after you have initialised config and before you call save

config.AppSettings.File = config.FilePath;

HTH

Upvotes: 0

Related Questions