如何从 .NET 中的 app.config 检索自定义部分?
How to retrieve a custom section from an app.config in .NET?
我有一个 .NET Framework 4.7.2 库项目,里面有一个 App.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<NewDocumentMetadata>
<add key="Type" value="principal"/>
<add key="IsActive" value="true"/>
</NewDocumentMetadata>
<appSettings>
<add key="Entity" value="9"/>
<add key="Flux" value="pdf"/>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- [...] -->
</assemblyBinding>
</runtime>
</configuration>
如您所见,我有一些标准设置,还有一个自定义部分。我的设置没有问题,但是当我检索该部分时,它可以工作,但是我被卡住了,当我尝试将其转换为 NameValueCollection
或 AppSettingsSection
时,它给了我一个 null
值,我遇到了无法使用的 ConfigurationOption
对象。
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
flux = appConfig.AppSettings.Settings["Flux"].Value; //Works
entity = appConfig.AppSettings.Settings["Entity"].Value; //Works
var metadataSection = appConfig.GetSection("NewDocumentMetadata"); //What do I do with this boy?
我需要检索 NewDocumentMetadata
部分中的设置,如何进行?
你应该试试这个:
var metadataSection = ConfigurationManager.GetSection("NewDocumentMetadata") as NameValueCollection;
// Get all the value foreach key
foreach(var key in metadataSection.AllKeys)
{
string value = metadataSection.GetValues(key).FirstOrDefault()
}
我刚刚发现问题出在这一行:
<section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" />
.
我更改了类型并为此做了 :
<section name="NewDocumentMetadata" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
现在我只需要执行这种代码来检索我想要的值:
var metadataSection = (AppSettingsSection)appConfig.GetSection("NewDocumentMetadata");
foreach (var key in metadataSection.Settings.AllKeys)
{
string value = metadataSection.Settings[key].Value;
}
我有一个 .NET Framework 4.7.2 库项目,里面有一个 App.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<NewDocumentMetadata>
<add key="Type" value="principal"/>
<add key="IsActive" value="true"/>
</NewDocumentMetadata>
<appSettings>
<add key="Entity" value="9"/>
<add key="Flux" value="pdf"/>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- [...] -->
</assemblyBinding>
</runtime>
</configuration>
如您所见,我有一些标准设置,还有一个自定义部分。我的设置没有问题,但是当我检索该部分时,它可以工作,但是我被卡住了,当我尝试将其转换为 NameValueCollection
或 AppSettingsSection
时,它给了我一个 null
值,我遇到了无法使用的 ConfigurationOption
对象。
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
flux = appConfig.AppSettings.Settings["Flux"].Value; //Works
entity = appConfig.AppSettings.Settings["Entity"].Value; //Works
var metadataSection = appConfig.GetSection("NewDocumentMetadata"); //What do I do with this boy?
我需要检索 NewDocumentMetadata
部分中的设置,如何进行?
你应该试试这个:
var metadataSection = ConfigurationManager.GetSection("NewDocumentMetadata") as NameValueCollection;
// Get all the value foreach key
foreach(var key in metadataSection.AllKeys)
{
string value = metadataSection.GetValues(key).FirstOrDefault()
}
我刚刚发现问题出在这一行:
<section name="NewDocumentMetadata" type="System.Configuration.NameValueSectionHandler" />
.
我更改了类型并为此做了 :
<section name="NewDocumentMetadata" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
现在我只需要执行这种代码来检索我想要的值:
var metadataSection = (AppSettingsSection)appConfig.GetSection("NewDocumentMetadata");
foreach (var key in metadataSection.Settings.AllKeys)
{
string value = metadataSection.Settings[key].Value;
}