繁体   English   中英

C#表单-以编程方式在.config文件中添加缺少的条目

[英]c# forms - Programatically add missing entrys in .config file

谷歌搜索了很长时间之后我没有找到答案(然后很长一段时间休息一下并再次搜索)...

说,我的应用程序设置中有2个设置。 String1和String2。 进一步说,我们交付了该产品,并开始添加次要功能(还有更多要配置的内容),并添加了String3。 如何在不手动遍历.config文件的情况下添加缺少的条目? 作为更新发布(不带OneClick顺带)时,现有的.config文件仅具有String1和String2。

在默认使用String3时,应用程序以某种方式理解缺少条目,因此应该或者可以将默认值添加到该设置中,以便另一个程序或用户都不会手动输入整个标签,却不知道它的真正名字。

提前致谢!

Qudeid

大家好

我刚刚整理了以下对我有用的代码。

稍微说明一下:我首先使用ConfigurationManager打开配置文件,获取相应的部分并将ForceSave设置为true,以便确保保存该部分。 然后,“魔术”开始。 我遍历程序集设置的所有属性,然后让linq神奇地发现元素是否存在。 如果没有,我创建它并将其附加到文件中。

注意 :这段代码仅适用于应用程序设置,不适用于用户设置,因为这是另一部分。 我还没有尝试过/测试过它,但它可能就像更改此行一样简单:

ConfigurationSectionGroup sectionGroup = configFile.SectionGroups["applicationSettings"];

到这一行:

ConfigurationSectionGroup sectionGroup = configFile.SectionGroups["userSettings"];

因为这是该部分的相应名称。 但是,没有任何保证。

这是我的代码:

/// <summary>
/// Loads own config file and compares its content to the settings, and adds missing entries with 
/// their default value to the file and saves it.
/// </summary>
private void UpdateSettings()
{
  // Load .config file
  Configuration configFile = ConfigurationManager.OpenExeConfiguration(typeof(Settings).Assembly.Location);

  // Get the wanted section
  ConfigurationSectionGroup sectionGroup = configFile.SectionGroups["applicationSettings"];
  ClientSettingsSection clientSettings = (ClientSettingsSection)sectionGroup.Sections[0];

  // Make sure the section really is saved later on
  clientSettings.SectionInformation.ForceSave = true;

  // Iterate through all properties
  foreach (SettingsProperty property in Settings.Default.Properties)
  {
    // if any element in Settings equals the property's name we know that it exists in the file
    bool exists = clientSettings.Settings.Cast<SettingElement>().Any(element => element.Name == property.Name);

    // Create the SettingElement with the default value if the element happens to be not there.
    if (!exists)
    {
      var element = new SettingElement(property.Name, property.SerializeAs);
      var xElement = new XElement(XName.Get("value"));
      XmlDocument doc = new XmlDocument();
      XmlElement valueXml = doc.ReadNode(xElement.CreateReader()) as XmlElement;
      valueXml.InnerText = property.DefaultValue.ToString();
      element.Value.ValueXml = valueXml;

      clientSettings.Settings.Add(element);
    }
  }

  // Save config
  configFile.Save();
}

在Visual Studio中创建设置时(项目->属性-> Settings.settings),您可以在设置编辑器中为该设置分配一个值。 根据设置定义(实际上是XML文件),将生成一个代码文件,其中包含一个类,该类使您可以访问设置。 此类默认情况下将使用在设置编辑器中分配给该设置的值。 但是,访问设置后,它将在App.config文件中查找该设置的值。 如果有一个值,它将覆盖代码生成文件中的默认值。

这意味着,如果您向项目中添加设置但未在App.config文件中提供该设置的值,则该设置的值将是在设置编辑器中分配的默认值。

要覆盖该值,请在应用程序的App.config文件中为其分配值。

由于您的应用程序可以拆分为多个项目创建的多个程序集,因此无法自动执行以下过程:在相关程序集中添加设置会为该设置为主项目的App.config文件创建一个条目。 恐怕你必须自己做。

但这恰恰是系统的优点:两个.exe项目可以依赖于定义设置的同一.dll项目。 在每个.exe项目中,您可以覆盖App.config文件中.exe项目的设置,也可以决定使用.dll项目定义的默认值。

如何添加新的“

[英]How to add a new “<location path=” with all settings to web.config programatically C#?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM