0

I am trying to save some configurations in app.config for my windows application. I searched in Google for solution but not anything related to this. My requirement is I want to save/update the configurations in app.config using my windows application.

My schema will be like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="customAppSettings1" type="System.Configuration.NameValueSectionHandler"/>
    <section name="customAppSettings2" type="System.Configuration.NameValueSectionHandler"/>    
  </configSections>
  <customAppSettings1>
    <add key="FirstKey" value="1" />
    <add key="SecondKey" value="2" />
  </customAppSettings1>
  <customAppSettings2>
    <add key="FirstKey" value="1" />
    <add key="SecondKey" value="2" />
  </customAppSettings2>
</configuration>
  • What? It's just XML. – lc. Aug 11 '15 at 06:12
  • Yes, I know how to read the elements but not found how to update the value of an element. – Avinash Patil Aug 11 '15 at 06:14
  • Possible duplicates - http://stackoverflow.com/questions/1357240/change-the-value-in-app-config-file-dynamically http://stackoverflow.com/questions/8522912/update-app-config-file-programatically-with-configurationmanager-openexeconfigur – Karthik Aug 11 '15 at 06:16
  • Above solution is for updating appSettings. I am asking for custom config section. – Avinash Patil Aug 11 '15 at 06:18

1 Answers1

0

This code is giving changed custom values. but not saving on physical file. A bit progress in your question.

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
xmlDoc.SelectSingleNode("//applicationSettings/MyApp_Application.Properties.Settings/setting/value").InnerText = "server=127.0.0.1;uid=root;pwd=root;database=" + comboBox1.SelectedItem.ToString();

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("applicationSettings/MyApp_Application.Properties.Settings/setting");
Umer Farooq
  • 221
  • 1
  • 4
  • 14