特定于装配的设置无法在运行时加载 [英] Assembly specific settings not loading at runtime
问题描述
我正在开发.NET 3.5 Windows Forms应用程序.我有两个项目,UI和一个库.
I am developing a .NET 3.5 Windows Forms app. I have two projects, the UI and a Library.
UI通常使用强类型设置,这些设置通常存储在app.config文件中.我使用UI.Properties.Settings类(由Visual Studio生成)阅读它们.
UI uses strongly typed settings that are stored, as usually, in the app.config file. I read them using UI.Properties.Settings class (generated by Visual Studio).
库使用其自己的强类型设置(转储到Library.config文件中的Settings.settings文件).
Library uses its own strongly typed Settings (the Settings.settings file that is dumped into Library.config file).
在运行时,库设置不会从Library.config文件中重新加载.运行时仅读取UI.config文件.因此,我坚持使用库"程序集中的默认设置,并且无法在部署后提供值.
At runtime, Library settings will not reload from the Library.config file. Only UI.config file is read by the runtime. So I am stuck with default settings in Library assembly, and cannot provide values after deployment.
总结:对于不是主要可执行文件的应用程序程序集,在程序集加载时不会读取设置.
To summarize: for an application assembly that is not the main executable, the settings are not read at assembly load time.
我知道我可以使用ConfigurationManager.AppSettings ["value"],这将从默认的应用程序配置文件(UI.config)中读取,但是如果我要使用强类型的设置(使用Properties.Settings类读取),该怎么办?
I know I can use ConfigurationManager.AppSettings["value"] and this will read from the default app config file (UI.config) but what can I do if I want strongly typed settings (read with Properties.Settings class)?
调用 Library.Properties.Settings.Default.Reload()
不会执行此操作.
谢谢.
推荐答案
您需要做的是将您的库配置节合并到app.connfig.合并配置文件是通过首先在< configSections >中添加元素来完成的config元素,以标识config部分,然后在配置元素内添加config元素.
What you need to do is merge your library config sections to app.connfig. Merging config files is done by first adding elements inside the <configSections> config element, to identity the config section and then by adding the config elements inside the configuration element.
合并配置文件的示例:
应用配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Settings>
<setting name="Setting" serializeAs="String">
<value>2</value>
</setting>
</CA3.Settings>
</userSettings>
</configuration>
库配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Library>
<setting name="Setting" serializeAs="String">
<value>1</value>
</setting>
</CA3.Library>
</userSettings>
</configuration>
包含库和应用程序配置的合并的app.config.
Merged app.config containing both library and app configuration.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Settings>
<setting name="Setting" serializeAs="String">
<value>2</value>
</setting>
</CA3.Settings>
<CA3.Library>
<setting name="Setting" serializeAs="String">
<value>1</value>
</setting>
</CA3.Library>
</userSettings>
</configuration>
这篇关于特定于装配的设置无法在运行时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!