从 DLL 访问 app.config
Access app.config from DLL
我读过 this post from Marc Gravell 展示如何从 DLL 读取 app.config
文件。当我尝试从该库中读取 userSettings
时,我得到一个空列表:
var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;
app.config
文件如下所示:
<?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="CodeGenerator.CodeGeneratorApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CodeGenerator.CodeGeneratorApp.Properties.Settings>
<setting name="CodeExtensions" serializeAs="Xml">
<value>
<CodeExtensionList>
<Extension AssemblyQualifiedName="CodeGenerator.Extensions.NamespaceTypesExtension, CodeGenerator, Culture=neutral">
<NamespaceTypesExtension />
</Extension>
...
</CodeExtensionList>
</value>
</setting>
</CodeGenerator.CodeGeneratorApp.Properties.Settings>
</userSettings>
</configuration>
我的 DLL 和应用程序位于同一个文件夹中,即使我添加
var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;
到 QuickWatch 我看到返回对象的 FilePath
设置为应用程序配置文件,所以它被找到并正确打开。
特别是我想要实现的是为 CodeExtensionList
反序列化 value
但我什至不知道如何访问文件中的那个部分。
看来你得到了你想要的。
您的 app.config 中没有 appSettings 部分。这就是为什么 AppSettings.Settings
returns 一个空列表。
编辑
您似乎正在尝试加载自定义部分 CodeGenerator.CodeGeneratorApp.Properties.Settings
。如果是这样,下面的代码应该可以完成工作:
var settings = ConfigurationManager
.OpenExeConfiguration("CodeGeneratorApp.exe")
.GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings")
在 klashar 的帮助下,我终于搞定了它。由于信息位于 userSettings
中,我们当然需要查询此特定部分。这可以通过以下代码完成:
var config = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe");
var sectionGroup = (ClientSettingsSection) config.GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings");
现在我们可以得到该组中的 SettingElement
,在我的例子中只有一个,所以我们可以取第一个,查询其内容为 Xml,写下 Xml 到 MemoryStream
和 de-serialize 那个流:
var setting = (SettingElement) sectionGroup.Settings[0];
var ser = new XmlSerializer(typeof(CodeExtensionList));
MemoryStream stm = new MemoryStream();
StreamWriter stw = new StreamWriter(stm);
stw.Write(setting.Value.ValueXml.InnerXml);
stw.Flush();
stm.Position = 0;
var value = ser.Deserialize(stm) as CodeExtensionList;
我读过 this post from Marc Gravell 展示如何从 DLL 读取 app.config
文件。当我尝试从该库中读取 userSettings
时,我得到一个空列表:
var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;
app.config
文件如下所示:
<?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="CodeGenerator.CodeGeneratorApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CodeGenerator.CodeGeneratorApp.Properties.Settings>
<setting name="CodeExtensions" serializeAs="Xml">
<value>
<CodeExtensionList>
<Extension AssemblyQualifiedName="CodeGenerator.Extensions.NamespaceTypesExtension, CodeGenerator, Culture=neutral">
<NamespaceTypesExtension />
</Extension>
...
</CodeExtensionList>
</value>
</setting>
</CodeGenerator.CodeGeneratorApp.Properties.Settings>
</userSettings>
</configuration>
我的 DLL 和应用程序位于同一个文件夹中,即使我添加
var settings = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe").AppSettings.Settings;
到 QuickWatch 我看到返回对象的 FilePath
设置为应用程序配置文件,所以它被找到并正确打开。
特别是我想要实现的是为 CodeExtensionList
反序列化 value
但我什至不知道如何访问文件中的那个部分。
看来你得到了你想要的。
您的 app.config 中没有 appSettings 部分。这就是为什么 AppSettings.Settings
returns 一个空列表。
编辑
您似乎正在尝试加载自定义部分 CodeGenerator.CodeGeneratorApp.Properties.Settings
。如果是这样,下面的代码应该可以完成工作:
var settings = ConfigurationManager
.OpenExeConfiguration("CodeGeneratorApp.exe")
.GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings")
在 klashar 的帮助下,我终于搞定了它。由于信息位于 userSettings
中,我们当然需要查询此特定部分。这可以通过以下代码完成:
var config = ConfigurationManager.OpenExeConfiguration("CodeGeneratorApp.exe");
var sectionGroup = (ClientSettingsSection) config.GetSection("userSettings/CodeGenerator.CodeGeneratorApp.Properties.Settings");
现在我们可以得到该组中的 SettingElement
,在我的例子中只有一个,所以我们可以取第一个,查询其内容为 Xml,写下 Xml 到 MemoryStream
和 de-serialize 那个流:
var setting = (SettingElement) sectionGroup.Settings[0];
var ser = new XmlSerializer(typeof(CodeExtensionList));
MemoryStream stm = new MemoryStream();
StreamWriter stw = new StreamWriter(stm);
stw.Write(setting.Value.ValueXml.InnerXml);
stw.Flush();
stm.Position = 0;
var value = ser.Deserialize(stm) as CodeExtensionList;