最简单的方法如下:
在 DLL 程序集的项目属性中添加所需的设置,就像对任何普通应用程序所做的那样。
现在,DLL 实际上并没有读取它自己的app.config
. 诀窍是将整个<applicationSettings>
块从 DLL复制app.config
到应用程序,并在文件顶部的部分中app.config
添加相应的行。<sectionGroup>
示例:假设app.config
您的 DLL 看起来像这样:
<?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="DLLSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="DLLSample.Properties.Settings.MyConnectionString" connectionString="Data Source=..." providerName="System.Data.SqlClient" />
</connectionStrings>
<applicationSettings>
<DLLSample.Properties.Settings>
<setting name="AllowStart" serializeAs="String">
<value>True</value>
</setting>
</DLLSample.Properties.Settings>
</applicationSettings>
</configuration>
你app.config
的应用程序看起来像这样:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="App.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<App.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\Temp</value>
</setting>
</App.Properties.Settings>
</applicationSettings>
</configuration>
完成上述更改后,应用程序的配置文件应如下所示:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="App.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="DLLSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="DLLSample.Properties.Settings.MyConnectionString" connectionString="Data Source=..." providerName="System.Data.SqlClient" />
</connectionStrings>
<applicationSettings>
<App.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\Temp</value>
</setting>
</App.Properties.Settings>
<DLLSample.Properties.Settings>
<setting name="AllowStart" serializeAs="String">
<value>True</value>
</setting>
</DLLSample.Properties.Settings>
</applicationSettings>
</configuration>
从您的应用程序和 DLL 代码中,您将能够使用正常的配置设置机制,例如通过Properties.Settings.Default.AllowStart
DLL 的代码或Properties.Settings.Default.LogPath
应用程序的代码访问一个值。
你不能做的两件事:
- 从您的应用程序访问 DLL 配置值,反之亦然
- 从应用程序的属性页管理 DLL 配置值。您需要
app.config
手动编辑以添加/删除/修改设置。
顺便说一下,连接字符串也可以这样管理。我已经添加到上面的示例中。