Read/Modify app.config file

Hi Experts,

I need to read/modify the app config file.  I have TestApp.exe.config with the
<value>http://localhost:8080/axis2/services/MobileUserFeatures</value>  information.  I need to write this based on user inputs.  I tried the following code to read, but I am getting null values.  How do I read and then modify this file.


public static string ReadSetting(string key)
    return ConfigurationManager.AppSettings[key];

<?xml version="1.0" encoding="utf-8" ?>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="TestApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <setting name="TestApp_localhost_MobileUserFeatures" serializeAs="String">
Who is Participating?
p_davisConnect With a Mentor Commented:
you could break that string up into parts and
use addKey
  <add key="host" value="localhost"/>
  <add key="port" value = "8080"/>
  .......and so on


then from your program where the user inserts info you can use

        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        KeyValueConfigurationElement kvce;
        Configuration config;
        public Form1()

            fileMap.ExeConfigFilename = fileMap.ExeConfigFilename  = "C:\\path to your\\TestApp.exe.config";
            config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None) as Configuration; ;
            kvce = config.AppSettings.Settings["host"];

            txtServerSet.Text = config.AppSettings.Settings["host"].Value.ToString();

        private void btnOK_Click(object sender, EventArgs e)

            kvce.Value = textBox.Text;

            config.Save(ConfigurationSaveMode.Minimal, true);

this of course only modifies one of the values but you can see how it works

quick and sloppy but it does work for modifying these settings
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.