Read/Modify app.config file

Posted on 2007-10-19
Medium Priority
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

ID: 20108995
LVL 22

Accepted Solution

p_davis earned 2000 total points
ID: 20109425
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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 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