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.
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.
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.
A typical location would be: C:\Windows\System32\inetsrv\ApplicationHost. config.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With