Loading Properties.Settings from a different file at runtime

前端 未结 3 1677
抹茶落季
抹茶落季 2021-01-01 01:13

Is there any way to load settings from a different file other than the default App.config file at runtime? I\'d like to do this after the default config file i

相关标签:
3条回答
  • 2021-01-01 01:41

    Look at using ExeConfigurationFileMap and ConfigurationManager.OpenMappedExeConfiguration.

    See Cracking the Mysteries of .Net 2.0 Configuration

    The ExeConfigurationFileMap allows you to specifically configure the exact pathnames to machine, exe, roaming and local configuration files, all together, or piecemeal, when calling OpenMappedExeConfiguration(). You are not required to specify all files, but all files will be identified and merged when the Configuration object is created. When using OpenMappedExeConfiguration, it is important to understand that all levels of configuration up through the level you request will always be merged. If you specify a custom exe and local configuration file, but do not specify a machine and roaming file, the default machine and roaming files will be found and merged with the specified exe and user files. This can have unexpected consequences if the specified files have not been kept properly in sync with default files.

    0 讨论(0)
  • 2021-01-01 01:49

    You can include the types so you don't need to manually update the source every time.

    `private void LoadSetting(System.Xml.Linq.XElement setting) { 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 讨论(0)
  • 2021-01-01 02:02

    It depends on the type of the application:

    1. Web Application & Windows Application - use the configSource xml attribute if you are willing to store the config files in the same folder (or subfolders) as the application
    2. Create a settings provider and also implement IApplicationSettingsProvider. Samples here and here. You might also need to use the IConfigurationManagerInternal interface to replace the default .NET configuration manager. When implementing the provider don't forget to make a difference between user settings and application settings and the roaming profiles.

    If you want to get started quickly just decompile the LocalFileSettingsProvider class (the default settings provider) and change it to your needs (you might find some useles code and might need to replicate all of the classes on which it depends).

    Good luck

    0 讨论(0)
提交回复
热议问题