1

i am trying to change App.Config file appsettings key value, everything works fine, while changing key value all comments are remove in config file (i want comments also), can anyone help me what's wrong with my code

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ConfigFilepath,
                ConfigurationUserLevel.None);
            config.AppSettings.Settings["IPAddress"].Value = "10.10.2.3";

            config.Save(ConfigurationSaveMode.Full);
svick
  • 236,525
  • 50
  • 385
  • 514
user1120572
  • 771
  • 1
  • 7
  • 7
  • 3
    possible duplicate of [Can ConfigurationManager retain XML comments on Save()?](http://stackoverflow.com/questions/1954358/can-configurationmanager-retain-xml-comments-on-save) – hybrid2102 Sep 15 '14 at 10:02
  • Check the answer on the link below: [https://stackoverflow.com/a/59215389/10148675](https://stackoverflow.com/a/59215389/10148675) – Ruzo Owzy Dec 06 '19 at 14:55

3 Answers3

4

This is how I solved this problem. In my case, the appSettings section was stored in a separate file from the web.config (using a configSource attribute).

public static void SaveAppSetting(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    SaveUsingXDocument(key, value, config.AppSettings.ElementInformation.Source);
}

/// <summary>
/// Saves the using an XDocument instead of ConfigSecion.
/// </summary>
/// <remarks>
/// The built-in <see cref="T:System.Configuration.Configuration"></see> class removes all XML comments when modifying the config file.
/// </remarks>
private static void SaveUsingXDocument(string key, string value, string fileName)
{
    XDocument document = XDocument.Load(fileName);
    if ( document.Root == null )
    {
        return;
    }
    XElement appSetting = document.Root.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
    if ( appSetting != null )
    {
        appSetting.Attribute("value").Value = value;
        document.Save(fileName);
    }
}
Jeremy Bell
  • 698
  • 5
  • 9
0

Based on @Jeremy_bell great answer, if you also want to add new setting if not exits you can do this:

public static void SaveAppSetting(string key, string value)
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    SaveUsingXDocument(key, value, config.AppSettings.ElementInformation.Source);
}

/// <summary>
/// Saves the using an XDocument instead of ConfigSecion.
/// </summary>
/// <remarks>
/// The built-in <see cref="T:System.Configuration.Configuration"></see> class removes all XML comments when modifying the config file.
/// </remarks>
private static void SaveUsingXDocument(string key, string value, string fileName)
{
    XDocument document = XDocument.Load(fileName);
    if ( document.Root == null )
    {
        return;
    }
    XElement appSetting = document.Root.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
    if ( appSetting != null )
    {
        appSetting.Attribute("value").Value = value;
        document.Save(fileName);
    }
    else
    {
        XElement el = new XElement("add");
        el.SetAttributeValue("key", key);
        el.SetAttributeValue("value", value);
        document.Root.Add(el);
        document.Save(fileName);
    }
}

I just added this:

else
    {
        XElement el = new XElement("add");
        el.SetAttributeValue("key", key);
        el.SetAttributeValue("value", value);
        document.Root.Add(el);
        document.Save(fileName);
    }
Yinon_90
  • 1,315
  • 12
  • 17
-3

Call in the end after save

ConfigurationManager.RefreshSection( "appSettings" );
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • thanks for your reply, actually i am trying to change one application app.config file from another exe, there i cannt use "OpenExeConfiguration" . – user1120572 Sep 11 '12 at 15:35
  • i'am happy to help you user1120571, call in the end ConfigurationManager.RefreshSection( "appSettings" ); – Aghilas Yakoub Sep 11 '12 at 15:42
  • 1
    Thanks Aghilas , after calling refershsection also all comments are removes in appsettings section. – user1120572 Sep 11 '12 at 15:57
  • I'am happy to help you user1120572 – Aghilas Yakoub Sep 11 '12 at 15:58
  • RefreshSection only refreshes the appSettings in the memory. You can't keep the comments, unless you change the file in a different Project in your solution as a generic text file which is a burden ad not recommended. – Frank Goortani Sep 11 '12 at 16:53