开发者

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

开发者 https://www.devze.com 2023-04-11 04:34 出处:网络
a question I haven\'t found an answer to after googling for a long time (then a long break from it and searching again)...

a question I haven't found an answer to after googling for a long time (then a long break from it and searching again)...

Say, I've got 2 Settings in my application settings. String1 and String2. Say, further, we shipped the product and start adding minor features (more things to configure) and we add a String3. How, without traversing the .config file manually, can I add missing entries? When shipped as an update (without OneClick btw.) the existing .config file only has String1 and String2.

While defaulting for String3, the application somehow understands that an entry is missing, so it ought to be possible, or so I think, to add this one setting w开发者_如何学运维ith the default value, so that either another program or a user, doesn't have to type the whole tags manually, without knowing what name it really is.

Thanks in advance!

Qudeid


Hi folks again,

I've just whipped up the following piece of code that works for me as I wanted.

Just to explain it a little: I first open the config file using the ConfigurationManager, get the corresponding section and set the ForceSave to true, so that section is sure to save. Then, the "magic" starts. I iterate through all properties of the assembly's settings and let linq do its magic to find if the element exists. If not, I create it and append it to the file.

Note: This piece of code is only for application settings, not so for user settings as this is a different section. I haven't tried/tested it, but it could be as simple as changing this line:

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

to this line:

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

as this is the corresponding name of that section. No guarantees, though.

Here's my code:

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


When you create a setting in Visual Studio (Project -> Properties -> Settings.settings) you assign a value to that setting in the settings editor. From the settings definition (really an XML file) a code file is generated with a class that gives you access to the settings. This class will as a default use the value assigned to the setting in the settings editor. However, when the setting is accessed it will look for a value of that setting in the App.config file. If there is a value it will override the default value in the code generated file.

What this means is that if you add a setting to your project but doesn't provide a value for that setting in the App.config file the value of the setting will be the default value assigned in the settings editor.

To override the value assign it in the App.config file for the application.

Because your application can be split into multiple assemblies created by multiple projects there is no way to automate a process where adding a setting in a dependent assembly creates an entry for that setting the App.config file for the main project. You have to do that yourself I'm afraid.

But that is exactly the beauty of the system: Two .exe projects can have a dependency on the same .dll project that defines a setting. In each .exe project you can override the setting in the App.config file for the .exe project or you can decide to use the default value defined by the .dll project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号