Click here to Skip to main content
15,884,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Coders,

I want to update the contents of my app.config file from other config file which is in a shared location. Please help me the coding part of it.

shared.config file
HTML
!appsettings!
!add key="loc1" value="location1"!
!/appsettings!

app.config file
HTML
!appsettings!
!add key="loc" value="location"!
!/appsettings!


now how can i reflect the change of location present in app.config file with the location present in shared.config file ,with button click. Thanks in advance.
Posted
Updated 12-Mar-12 23:18pm
v2
Comments
Oshtri Deka 13-Mar-12 3:53am    
Off-topic, but I must ask, why don't you use xml based config solutions?
Herman<T>.Instance 13-Mar-12 5:32am    
have you read this?

1 solution

this is the code to update the app.config file.
once you modify the app.config file the changes will reflect if you restart the application
you can call the function like this

SQL
AppSettingsUpdater.UpdateConfigFile("loginid", "newdetails");
                                AppSettingsUpdater.UpdateConfigFile("pwd", newdetails2);


your code should be like this


C#
public class AppSettingsUpdater
   {
       public static void UpdateConfigFile(string key, string value)
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
           foreach (XmlElement element in xmlDoc.DocumentElement)
               if (element.Name == "appSettings")
               {
                   foreach (XmlNode node in element.ChildNodes)
                       if (node.Attributes[0].Value == key)
                           node.Attributes[1].Value = value;
               }
           xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
           ConfigurationManager.RefreshSection("appSettings");
       }
   }




your app.config file should be like this


XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="loginid" value="NODATA"></add>
    <add key="pwd" value="NODATA"></add>
  </appSettings>
</configuration>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900