C# 更改appsettings键时删除配置文件注释

C# 更改appsettings键时删除配置文件注释,c#,app-config,C#,App Config,我正在尝试更改App.Config文件appsettings键值,一切正常,更改键值时,所有注释都会在配置文件中删除(我也需要注释),有人能帮我解决代码的问题吗 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ConfigFilepath, ConfigurationUserLevel.None); config.AppSettings.Setti

我正在尝试更改App.Config文件appsettings键值,一切正常,更改键值时,所有注释都会在配置文件中删除(我也需要注释),有人能帮我解决代码的问题吗

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

            config.Save(ConfigurationSaveMode.Full);

保存后最后调用

ConfigurationManager.RefreshSection( "appSettings" );

保存后最后调用

ConfigurationManager.RefreshSection( "appSettings" );

这就是我解决这个问题的方法。在我的例子中,appSettings部分存储在web.config的单独文件中(使用configSource属性)

publicstaticvoidsaveappsetting(字符串键、字符串值)
{
Configuration config=WebConfigurationManager.OpenWebConfiguration(“~”);
SaveUsingXDocument(key、value、config.AppSettings.ElementInformation.Source);
}
/// 
///使用XDocument而不是ConfigSecion保存。
/// 
/// 
///修改配置文件时,内置类将删除所有XML注释。
/// 
私有静态void SaveUsingXDocument(字符串键、字符串值、字符串文件名)
{
XDocument document=XDocument.Load(文件名);
if(document.Root==null)
{
返回;
}
XElement appSetting=document.Root.Elements(“添加”).FirstOrDefault(x=>x.Attribute(“key”).Value==key);
if(appSetting!=null)
{
appSetting.Attribute(“value”).value=value;
document.Save(文件名);
}
}

我就是这样解决这个问题的。在我的例子中,appSettings部分存储在web.config的单独文件中(使用configSource属性)

publicstaticvoidsaveappsetting(字符串键、字符串值)
{
Configuration config=WebConfigurationManager.OpenWebConfiguration(“~”);
SaveUsingXDocument(key、value、config.AppSettings.ElementInformation.Source);
}
/// 
///使用XDocument而不是ConfigSecion保存。
/// 
/// 
///修改配置文件时,内置类将删除所有XML注释。
/// 
私有静态void SaveUsingXDocument(字符串键、字符串值、字符串文件名)
{
XDocument document=XDocument.Load(文件名);
if(document.Root==null)
{
返回;
}
XElement appSetting=document.Root.Elements(“添加”).FirstOrDefault(x=>x.Attribute(“key”).Value==key);
if(appSetting!=null)
{
appSetting.Attribute(“value”).value=value;
document.Save(文件名);
}
}

基于@Jeremy_bell伟大的答案,如果您还想添加新设置,如果不退出,您可以这样做:

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);
    }
}

基于@Jeremy_bell伟大的回答,如果您还想添加新设置,如果不退出,您可以这样做:

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);
    }
}

感谢您的回复,实际上我正在尝试从另一个exe更改一个应用程序app.config文件,在那里我不能使用“OpenExeConfiguration”。我很乐意帮助您使用用户1120571,在end ConfigurationManager.RefreshSection(“appSettings”);谢谢Aghilas,在调用refereshSection之后,appsettings部分中的所有注释都将被删除。我很高兴帮助您用户1120572RefreshSection只刷新内存中的appsettings。您不能保留评论,除非您将解决方案中其他项目中的文件更改为通用文本文件,这是一个不推荐的负担。感谢您的回复,实际上我正在尝试从另一个exe更改一个应用程序app.config文件,在那里我不能使用“OpenExeConfiguration”。我很高兴帮助您使用1120571,调用end ConfigurationManager.RefreshSection(“appSettings”);谢谢Aghilas,在调用refereshSection之后,appsettings部分中的所有注释都将被删除。我很高兴帮助您用户1120572RefreshSection只刷新内存中的appsettings。您无法保留注释,除非您将解决方案中其他项目中的文件更改为通用文本文件,这是一种负担,不建议使用。下面链接上的检查答案可能重复:下面链接上的检查答案可能重复: