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 655613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:37:24+00:00 2026-05-13T22:37:24+00:00

There are questions pertaining to reading settings from a separate config file and others

  • 0

There are questions pertaining to reading settings from a separate config file and others similar to it, but my question is specific to application property settings (i.e. <MyApplication.Properties.Settings> – see XML file below) and how to load them dynamically. I tried the method in this post, which involved refreshing the entire appSettings section of the main config file, but my adaptation threw exceptions because I wasn’t replacing the appSettings section:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
// Have tried the other ConfigurationUserLevels to no avail
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified); // throws ConfigurationErrorsException
ConfigurationManager.RefreshSection("userSettings");

The ConfigurationErrorsException.Message is “The root element must match the name of the section referencing the file, ‘appSettings’ (C:\myFile.xml line 2).” The file is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <MyApplication.Properties.Settings>
            <setting name="SineWaveFrequency" serializeAs="String">
                <value>6</value>
            </setting>
            <setting name="SineWaveAmplitude" serializeAs="String">
                <value>6</value>
            </setting>
        </MyApplication.Properties.Settings>
    </userSettings>
</configuration>

Is there a way to import the values from this file into the MyApplication.Properties.Settings.Default class, with the framework handling all XML deserialization like it does when the config file is loaded on application startup?

  • 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-13T22:37:25+00:00Added an answer on May 13, 2026 at 10:37 pm

    Well, this works:

    using System;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using System.Xml.XPath;
    
    public static class SettingsIO
    {
        internal static void Import(string settingsFilePath)
        {
            if (!File.Exists(settingsFilePath))
            {
                throw new FileNotFoundException();
            }
    
            var appSettings = Properties.Settings.Default;
            try
            {
                var config = 
    ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.PerUserRoamingAndLocal);
    
                string appSettingsXmlName = 
    Properties.Settings.Default.Context["GroupName"].ToString(); 
    // returns "MyApplication.Properties.Settings";
    
                // Open settings file as XML
                var import = XDocument.Load(settingsFilePath);
                // Get the whole XML inside the settings node
                var settings = import.XPathSelectElements("//" + appSettingsXmlName);
    
                config.GetSectionGroup("userSettings")
                    .Sections[appSettingsXmlName]
                    .SectionInformation
                    .SetRawXml(settings.Single().ToString());
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("userSettings");
    
                appSettings.Reload();
            }
            catch (Exception) // Should make this more specific
            {
                // Could not import settings.
                appSettings.Reload(); // from last set saved, not defaults
            }
        }
    
        internal static void Export(string settingsFilePath)
        {
            Properties.Settings.Default.Save();
            var config = 
    ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.PerUserRoamingAndLocal);
            config.SaveAs(settingsFilePath);
        }
    }
    

    The export method creates a file like the following:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <MyApplication.Properties.Settings>
                <setting name="SineWaveFrequency" serializeAs="String">
                    <value>1</value>
                </setting>
                <setting name="SineWaveAmplitude" serializeAs="String">
                    <value>100</value>
                </setting>
                <setting name="AdcShift" serializeAs="String">
                    <value>8</value>
                </setting>
                <setting name="ControlBitsCheckedIndices" serializeAs="String">
                    <value>0,1,2,3,5,6,7,8</value>
                </setting>
                <setting name="UpgradeSettings" serializeAs="String">
                    <value>False</value>
                </setting>
            </MyApplication.Properties.Settings>
        </userSettings>
    </configuration>
    

    The import method parses that file, takes the everything inside the node, puts that XML into the user.config file at the appropriate section, then reloads the Properties.Settings.Default in order to grab those values from the new user.config file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are questions similar to this one, but I've not found a
There are other questions on here that sound similar but are not. I have
There are similar questions, but none answers what I really need. I actually don't
There are similar questions to this, but I don't think anyone has asked this
This question is quite popular, and there are already lot of questions pertaining to
I have found several questions and answers pertaining to my question, but I'm getting
I realize there are several other questions pertaining to this - but either they
There are questions on this already, but they don't answer my question. For example:
I know that there are currently questions on SO already pertaining to saving jQuery
There are many questions like this but I can't find one that seems to

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.