gpt4 book ai didi

c# - 在运行时从不同的文件加载 Properties.Settings

转载 作者:太空狗 更新时间:2023-10-29 23:40:27 26 4
gpt4 key购买 nike

除了默认的 App.config 文件之外,有什么方法可以在运行时从不同的文件加载设置吗?我想在加载默认配置文件后执行此操作。

我使用 Visual Studio 中的 Settings.Settings GUI 为我创建 App.config 文件。配置文件最终看起来像这样:

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

在代码中,我可以像这样访问设置:

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

我的想法是,当应用程序运行时,我应该能够在运行时指定一个配置文件,并让应用程序将配置文件加载到 Properties.Settings.Default 对象中,而不是使用默认的 app.config 文件。配置文件的格式相同,但设置值不同。

我知道使用 ConfigurationManager.OpenExeConfiguration(configFile); 可以做到这一点。但是,在我运行的测试中,它不会更新 Properties.Settings.Default 对象以反射(reflect)配置文件中的新值。


经过较长时间的思考,我想出了一个我更喜欢的解决方案。我确信它有一些缺陷,但我认为它可以满足我需要它做的事情。

本质上,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's 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()

希望这对其他人有帮助!

最佳答案

查看使用 ExeConfigurationFileMap 和 ConfigurationManager.OpenMappedExeConfiguration。

参见 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.

关于c# - 在运行时从不同的文件加载 Properties.Settings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com