Click here to Skip to main content
13,091,145 members (51,265 online)
Rate this:
Please Sign up or sign in to vote.
I am using Visual Studio, after searching i've found this following code to modify app.config file dynamically
Configuration configFile = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
configFile.AppSettings.Settings["Servername"].Value = "Server-PC";

But while applying this in pmy project i am getting this following error

Error 35 The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)
Error 36 The name 'ConfigurationManager' does not exist in the current context

Can anyone please suggest me how to change app.config value dynamically?
Posted 18-Nov-12 17:47pm
Updated 18-Nov-12 18:10pm
Sergey Alexandrovich Kryukov 18-Nov-12 23:59pm
Wrong question. Why would you change it? It is not designed for such things. You got this wrong idea having some purpose in mind. Explain this purpose to get help on more reasonable approach. Always explain your ultimate goals when asking questions.
biswarup88 19-Nov-12 0:09am
Actually i want to change the database server name frequently; and i've stored the name in app.config file so i need to chage the app.config value.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Add Reference of
in your project.
if you added this reference then add below namespace on your page
using System.Configuration;
biswarup88 19-Nov-12 0:09am
i've added but still getting the error.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Finally i've got the solution
check this link[^]

// Open App.Config of executable
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // Add an Application Setting.
            config.AppSettings.Settings.Add("connection", server);
            // Save the configuration file.
            // Force a reload of a changed section.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100