Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:19:04+00:00 2026-05-26T03:19:04+00:00

a question I haven’t found an answer to after googling for a long time

  • 0

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 with 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:19:05+00:00Added an answer on May 26, 2026 at 3:19 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question I haven't found an answer for by searching... How do
I haven't found an answer to this particular question; perhaps there isn't one. But
It might be simple question but I haven't found the clear answer. I am
I haven't found the answer to this question anywhere and I'm a bit confused..
this may be a truly stupid question but I haven't readily found the answer.
My question may seem quite simple but I haven't found the answer yet. In
One simple question (but I haven't quite found an obvious answer in the NLP
I haven't found this question, feel free to close if it's already up here.
Simple question, but I haven't found a good explanation on google. When using Set
I've asked this question over on the MSDN forums also and haven't found a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.