Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common app.config between applications

Tags:

c#

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.

like image 942
klashagelqvist Avatar asked Mar 15 '12 13:03

klashagelqvist


People also ask

What does application configuration mean?

An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.

What is difference between web config and app config?

config is parsed at runtime, so if you edit the web. config file, the web application will automatically load the changes in the config file. Â app. config is parsed at compile time, so if you edit the app.

Where should app config be located?

A typical location would be: C:\Windows\System32\inetsrv\ApplicationHost. config.

How does app config work?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


1 Answers

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);
like image 90
AGuyCalledGerald Avatar answered Oct 03 '22 14:10

AGuyCalledGerald