Click here to Skip to main content
Rate this: bad
good
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";
            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 18-Nov-12 18:47pm
Edited 18-Nov-12 19:10pm
v2
Comments
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.
--SA
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
good
Please Sign up or sign in to vote.

Solution 1

Add Reference of
System.Configuration
in your project.
if you added this reference then add below namespace on your page
using System.Configuration;
  Permalink  
Comments
biswarup88 at 19-Nov-12 0:09am
   
i've added but still getting the error.
Rate this: bad
good
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.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");
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


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