?
Solved

Read/Modify app.config file

Posted on 2007-10-19
2
Medium Priority
?
1,732 Views
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.

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>
0
Comment
Question by:ambuli
2 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20108995
Try
this["TestApp_localhost_MobileUserFeatures"]
0
 
LVL 22

Accepted Solution

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

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