Read/Modify app.config file

Posted on 2007-10-19
Last Modified: 2013-12-17
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">
Question by:ambuli
    LVL 21

    Expert Comment

    LVL 22

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now