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.

ReadSetting("name");

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



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

</appSettings>

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

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

            InitializeComponent();
            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
0
 
surajgupthaCommented:
Try
this["TestApp_localhost_MobileUserFeatures"]
0
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.