1

I have the app.config file like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appsettings>
   <add key="ServiceName" value="MyService1" />
   <add key="URL" value="https://mydomain.com/test/main.asmx" />
   ...
</appsettings>
</configuration>

=================================================================

Now what I need to do from application, I need to add the proxy settings to the config file, on some special UI events.

So, the app.config file will look like this--

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appsettings>
   <add key="ServiceName" value="MyService1" />
   <add key="URL" value="https://mydomain.com/test/main.asmx" />
   ...
</appsettings>
<system.net>
 <defaultproxy>
   <proxy scriptlocation ="https://mysecuredomain.com/conf/proxy.pac" />
 </defaultproxy>
</system.net>
</configuration>

Your help will be appreciated.

Thanks

2
  • Right click project on project explorer, click properties and add your property there. Commented Jun 25, 2014 at 6:24
  • user can add / modify the value for the proxy script location, it will be dynamic, like in UI button click Commented Jun 25, 2014 at 6:33

2 Answers 2

1

App.config can be modified programatically like this:

If you want to add new key then use this:

    private void ModifyConfig(string key, string value)
    {
        // Open App.Config of executable
        System.Configuration.Configuration config =
          ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        // Add an Application Setting.
        config.AppSettings.Settings.Add(key, value);
        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified, true);
        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");
    }

If you want to update existing key then use this:

private void UpdateConfig(string key, string value)
{
    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings[key].Value = value;
    config.Save(ConfigurationSaveMode.Modified, true);
    ConfigurationManager.RefreshSection("appSettings");
}

Other sections can be modified in the similar manner.

I have found an excellent link which will guide you, how to update custom sections as well.

Update AppSettings and custom configuration sections in App.config at runtime

Sign up to request clarification or add additional context in comments.

2 Comments

I ahve to make it generalize to edit the appsettings key value and also the section for <system.net> outside of the <appsettings>. And need to save the part at a go. Where the value of the proxy script location is the user input. <system.net> <defaultproxy> <proxy scriptlocation ="mysecuredomain.com/conf/proxy.pac" /> </defaultproxy> </system.net>
Follow the link in the answer. It will guide you how to update sections other than appsettings. Also, if you want to make it generalized for AppSettings section, you need to pass key name and values as a parameter in the function. Change the function accordingly.
0

as I understand you need to be able to change the 'scriptlocation' value dynamically.

in your original (project source code version), you have tokens instead of the actual script location url address. For example:

Instead of:<proxy scriptlocation ="https://mysecuredomain.com/conf/proxy.pac" />

You should use: <proxy scriptlocation ="[ScriptLocationProxy]" />

where [ScriptLocationProxy] is a token to be changed pragmatically later. In your deployment script, look for this token and replace them (simple String Replace).

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.