Loading properties. Setting from another file at runtime

Is there a way to load settings from a file other than the App.configdefault file at runtime? I would like to do this after loading the default configuration file.

I use the GUI Settings.Settingsin Visual Studio to create my file App.configfor me. The configuration file is as follows:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="SnipetTester.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    </configSections>
      <applicationSettings>
        <SnipetTester.Properties.Settings>
          <setting name="SettingSomething" serializeAs="String">
            <value>1234</value>
          </setting>
        </SnipetTester.Properties.Settings>
      </applicationSettings>
    </configuration>

In the code, I can access the following settings:

Console.WriteLine("Default setting value:  " + Properties.Settings.Default.SettingSomething);

The idea is that when I start the application, I have to specify the configuration file at run time and attach the application to the configuration file in the object Properties.Settings.Defaultinstead of using the App.configdefault file . The configuration file formats will be the same, but the parameter values ​​will be different.

ConfigurationManager.OpenExeConfiguration(configFile);. , , Properties.Settings.Default, .


, , . , , , , .

, Properties.Settings Visual Studio; . , , . :

internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 
{
    //Parses a config file and loads its settings
    public void Load(string filename)
    {
        System.Xml.Linq.XElement xml = null;
        try
        {
            string text = System.IO.File.ReadAllText(filename);
            xml = System.Xml.Linq.XElement.Parse(text);
        }
        catch
        {
            //Pokemon catch statement (gotta catch 'em all)

            //If some exception occurs while loading the file,
            //assume either the file was unable to be read or
            //the config file is not in the right format.
            //The xml variable will be null and none of the
            //settings will be loaded.
        }

        if(xml != null)
        {
            foreach(System.Xml.Linq.XElement currentElement in xml.Elements())
            {
                switch (currentElement.Name.LocalName)
                {
                    case "userSettings":
                    case "applicationSettings":
                        foreach (System.Xml.Linq.XElement settingNamespace in currentElement.Elements())
                        {
                            if (settingNamespace.Name.LocalName == "SnipetTester.Properties.Settings")
                            {
                                foreach (System.Xml.Linq.XElement setting in settingNamespace.Elements())
                                {
                                    LoadSetting(setting);
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }

    //Loads a setting based on it xml representation in the config file
    private void LoadSetting(System.Xml.Linq.XElement setting)
    {
        string name = null, type = null, value = null;

        if (setting.Name.LocalName == "setting")
        {
            System.Xml.Linq.XAttribute xName = setting.Attribute("name");
            if (xName != null)
            {
                name = xName.Value;
            }

            System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs");
            if (xSerialize != null)
            {
                type = xSerialize.Value;
            }

            System.Xml.Linq.XElement xValue = setting.Element("value");
            if (xValue != null)
            {
                value = xValue.Value;
            }
        }


        if (string.IsNullOrEmpty(name) == false &&
            string.IsNullOrEmpty(type) == false &&
            string.IsNullOrEmpty(value) == false)
        {
            switch (name)
            {
                //One of the pitfalls is that everytime you add a new
                //setting to the config file, you will need to add another
                //case to the switch statement.
                case "SettingSomething":
                    this[name] = value;
                    break;
                default:
                    break;
            }
        }
    }
}

Properties.Settings.Load(string filename). . , . , Properties.Settings.Reload().

, - !

+5
3
+1

ExeConfigurationFileMap ConfigurationManager.OpenMappedExeConfiguration.

. .Net 2.0

ExeConfigurationFileMap , exe, , , OpenMappedExeConfiguration(). , , . OpenMappedExeConfiguration, , , , . exe , , exe . , .

0

, .

`private void LoadSetting (setting System.Xml.Linq.XElement) {string name = null, type = null; string value = null;

        if (setting.Name.LocalName == "setting")
        {
            System.Xml.Linq.XAttribute xName = setting.Attribute("name");
            if (xName != null)
            {
                name = xName.Value;
            }

            System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs");
            if (xSerialize != null)
            {
                type = xSerialize.Value;
            }

            System.Xml.Linq.XElement xValue = setting.Element("value");
            if (xValue != null)
            {
                if (this[name].GetType() == typeof(System.Collections.Specialized.StringCollection))
                {
                    foreach (string s in xValue.Element("ArrayOfString").Elements())
                    {
                        if (!((System.Collections.Specialized.StringCollection)this[name]).Contains(s))
                            ((System.Collections.Specialized.StringCollection)this[name]).Add(s);
                    }
                }
                else
                {
                    value = xValue.Value;
                }

                if (this[name].GetType() == typeof(int))
                {
                    this[name] = int.Parse(value);
                }
                else if (this[name].GetType() == typeof(bool))
                {
                    this[name] = bool.Parse(value);
                }
                else
                {
                    this[name] = value;
                }

            }
        }`
0
source

All Articles