Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 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";
configFile.Save();

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

Quote:
Error 35 The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)
and
Quote:
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
Updated 18-Nov-12 18:10pm
v2
Comments
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.
--SA
sahabiswarup 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.

Add Reference of
C#
System.Configuration
in your project.
if you added this reference then add below namespace on your page
C#
using System.Configuration;
 
Share this answer
 
Comments
sahabiswarup 19-Nov-12 0:09am    
i've added but still getting the error.
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.Remove("connection");
            config.AppSettings.Settings.Add("connection", server);
            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900