Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: 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 18:47pm
Edited 18-Nov-12 19: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 390
1 Maciej Los 205
2 Peter Leow 140
3 Suvendu Shekhar Giri 134
4 Abhinav S 126
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,895
2 Peter Leow 5,044
3 Kornfeld Eliyahu Peter 3,333
4 Maciej Los 2,561

Advertise | Privacy | Mobile
Web02 | 2.8.150327.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