如何动态加载单独的应用程序设置文件并与当前设置合并?
有一些与从单独的配置文件中读取设置有关的问题和其他类似的问题它,但我的问题特定于应用程序属性设置(即
- 请参阅下面的 XML 文件)以及如何动态加载它们。我尝试了这篇文章中的方法,其中涉及刷新主配置文件的整个 appSettings 部分,但我的改编引发了异常,因为我没有替换 appSettings 部分:
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");
ConfigurationErrorsException.Message 是“根元素必须与引用文件“appSettings”的部分的名称匹配(C:\myFile.xml 第 2 行)。 ”该文件是:
<?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>
有没有办法将此文件中的值导入到 MyApplication.Properties.Settings.Default
类中,框架处理所有 XML 反序列化,就像加载配置文件时一样应用程序启动?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这是可行的:
导出方法创建一个如下所示的文件:
导入方法解析该文件,获取节点内的所有内容,将该 XML 放入 user.config 文件的适当部分,然后重新加载 Properties.Settings。默认是为了从新的 user.config 文件中获取这些值。
Well, this works:
The export method creates a file like the following:
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.
Pat建议的解决方案:
返回
null
。我将其更改为并且效果完美。
The solution suggested by Pat:
returns
null
. I changed it toAnd it works perfectly.