28

I want to modify a value in appSetting section in app.config. So i wrote,

Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();
Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
config.AppSettings.Settings["name"].Value = "raja";       
config.Save(ConfigurationSaveMode.Modified);  
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();

after the execution of above code, i verified the app.config whether the value of "name" element has been changed or not. but no change.

what is the wrong with my code? or is there any other way to do this?

Partha
  • 2,062
  • 6
  • 24
  • 31
  • 3
    It changes only in in-memory level. it is not updating the physical level(in file level) – Partha Aug 31 '09 at 12:48
  • 3
    You should not do that. Application level settings should not be modified (that's why there's no support for that), but you should use user level settings instead. – Thorsten Dittmar Aug 31 '09 at 13:31
  • Check this link. It should solve your problem. http://stackoverflow.com/questions/11149556/c-sharp-app-config-change-value – Indira Jun 24 '14 at 20:43

6 Answers6

56

This code works for me:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    config.AppSettings.Settings["test"].Value = "blah";       
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");

Note: it doesn't update the solution item 'app.config', but the '.exe.config' one in the bin/ folder if you run it with F5.

Adis H
  • 624
  • 4
  • 5
  • 2
    This does not work. It still does not show that the settings have been changed until I next rerun the app. Not sure what is going on. – Nathan McKaskle Oct 20 '15 at 21:59
  • "Access to the path is denied" when i tried to install it. But when i try it in visual studio it works fine. Can you help me. – Reynan Nov 02 '16 at 11:12
  • I see no significant difference in the code so I assume the important part of this is the location (name) of the file updated. – Sam Hobbs Jan 21 '18 at 16:03
  • This helped a lot. Settings is a dictionary that you can add new key value pairs too. – Jeremy Ray Brown Feb 06 '19 at 18:27
6

You have to update your app.config file manually

// Load the app.config file
XmlDocument xml = new XmlDocument();
xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

// Do whatever you need, like modifying the appSettings section

// Save the new setting
xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

And then tell your application to reload any section you modified

ConfigurationManager.RefreshSection("appSettings");
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
  • 5
    Be aware of any security issue of updating a file that will most probably be located in Program Files. Under Vista, you need elevated permission to write to a file in Program Files. – Pierre-Alain Vigeant Aug 31 '09 at 13:18
6

Expanding on Adis H's example to include the null case (got bit on this one)

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings["HostName"] != null)
                config.AppSettings.Settings["HostName"].Value = hostName;
            else                
                config.AppSettings.Settings.Add("HostName", hostName);                
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
CCondron
  • 1,926
  • 17
  • 27
1

It works, just look at the bin/Debug folder, you are probably looking at app.config file inside project.

Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
1

Try:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("configFilePath");
config.AppSettings.Settings.Add("configFilePath", configFilePath);
config.Save(ConfigurationSaveMode.Modified,true);
config.SaveAs(@"C:\Users\USERNAME\Documents\Visual Studio 2010\Projects\ADI2v1.4\ADI2CE2\App.config",ConfigurationSaveMode.Modified, true); 
AviZ
  • 21
  • 5
0
 XmlReaderSettings _configsettings = new XmlReaderSettings();
 _configsettings.IgnoreComments = true;

 XmlReader _configreader = XmlReader.Create(ConfigFilePath, _configsettings);
 XmlDocument doc_config = new XmlDocument();
 doc_config.Load(_configreader);
 _configreader.Close();

 foreach (XmlNode RootName in doc_config.DocumentElement.ChildNodes)
 {
     if (RootName.LocalName == "appSettings")
     {
         if (RootName.HasChildNodes)
         {
             foreach (XmlNode _child in RootName.ChildNodes)
             {
                 if (_child.Attributes["key"].Value == "HostName")
                 {
                     if (_child.Attributes["value"].Value == "false")
                         _child.Attributes["value"].Value = "true";
                 }
             }
         }
     }
 }
 doc_config.Save(ConfigFilePath);
Matt Hamsmith
  • 3,955
  • 1
  • 27
  • 42