C# 在运行时从其他文件加载属性.设置
在运行时,除了默认的C# 在运行时从其他文件加载属性.设置,c#,app-config,C#,App Config,在运行时,除了默认的App.config文件外,是否有其他方法从其他文件加载设置?我希望在加载默认配置文件后执行此操作 我使用Visual Studio中的Settings.SettingsGUI为我创建App.config文件。配置文件的最终外观如下所示: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> &
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
对象以反映配置文件中的新值
在考虑了很久之后,我终于想出了一个我更喜欢的解决方案。我肯定它有一些陷阱,但我认为它会为我所需要的工作
本质上,Properties.Settings
类是由VisualStudio自动生成的;它为您生成类的代码。我能够找到代码的生成位置,并添加了一些函数调用,以自行加载配置文件。这是我的补充:
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(字符串文件名)
函数。该函数接受配置文件名作为参数。它将解析文件并加载在配置文件中遇到的任何设置。要恢复到原始配置,只需调用Properties.Settings.Reload()
希望这能帮助别人 查看如何使用ExeConfigurationFileMap和ConfigurationManager.OpenMappedExeConfiguration
看
ExeConfigurationFileMap允许您专门配置
计算机、exe、漫游和本地配置的精确路径名
调用时,所有文件都在一起,或是零碎的
OpenMappedExconfiguration()。您不需要指定所有
文件,但当
已创建配置对象。使用时
OpenMappedEconfiguration,了解所有
通过您请求的级别,配置级别将始终处于上升状态
合并。如果指定自定义exe和本地配置文件,
但不要指定机器和漫游文件,默认机器和
将找到漫游文件并与指定的exe和用户合并
文件夹。如果指定的文件
未与默认文件正确保持同步
它取决于应用程序的类型:
Web应用程序和Windows应用程序-如果您愿意将配置文件存储在与应用程序相同的文件夹(或子文件夹)中,请使用xml属性
创建并实现。样品和样品。您可能还需要使用该接口来替换默认的.NET配置管理器。在实现提供商时,不要忘记区分用户设置、应用程序设置和漫游配置文件
如果您想快速入门,只需反编译类(默认设置提供程序)并根据您的需要进行更改(您可能会找到一些有用的代码,并且可能需要复制它所依赖的所有类)
祝您好运您可以包含这些类型,这样就不必每次都手动更新源代码
`专用void加载设置(System.Xml.Linq.XElement设置)
{
字符串名称=null,类型=null;
字符串值=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;
}
}
}`
这看起来很有希望,但是我们如何使用类型呢?我们可以为任意类型处理这个问题吗