Click here to Skip to main content
11,804,574 members (93,545 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Visual-Studio VS2010
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
Edited 18-Nov-12 18:10pm
Sergey Alexandrovich Kryukov at 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 at 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 at 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
0 OriginalGriff 3,140
1 Maciej Los 1,910
2 KrunalRohit 1,907
3 CPallini 1,845
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2015
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