And*_*ndy 57 .net c# configuration app-config configurationsection
我想使用我的应用程序配置存储2个公司的设置,我更喜欢是否可以使用一个部分来分隔另一个的数据而不是给它们不同的密钥名称.
我一直在网上查看,但当人们使用部分或找到过时的简单方法来使用它时,我似乎有点不知所措.有人可以通过我的初学者指南吗?
下面是我的app.config的示例:
<configSections>
<section name="FBI" type="" />
<section name="FSCS" type="" />
</configSections>
<FSCS>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>
Run Code Online (Sandbox Code Playgroud)
更新:
基于anwer的高级解决方案.如果有人想知道.
App.config中:
<configuration>
<configSections>
<sectionGroup name="FileCheckerConfigGroup">
<section name="FBI" type="System.Configuration.NameValueSectionHandler" />
<section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<FileCheckerConfigGroup>
<FSCS>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>
</FileCheckerConfigGroup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
码:
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the collection of the section groups.
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
if (sectionGroup.Name == "FileCheckerConfigGroup")
{
foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
{
var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
inputDirectory = section["inputDirectory"]; //"C:\\testfiles";
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 80
<configSections>
<section name="FBI" type="System.Configuration.NameValueSectionHandler" />
<section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<FSCS>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>
Run Code Online (Sandbox Code Playgroud)
然后:
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
Run Code Online (Sandbox Code Playgroud)
小智 11
App.config中
<configSections>
<sectionGroup name="FileCheckers">
<section name="FBI" type="System.Configuration.NameValueSectionHandler" />
<section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<FileCheckers>
<FSCS>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>
</FileCheckers>
Run Code Online (Sandbox Code Playgroud)
示例用法
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup fileCheckersGroup = config.SectionGroups["FileCheckers"];
foreach (ConfigurationSection section in fileCheckersGroup.Sections)
{
NameValueCollection sectionSettings = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as NameValueCollection;
var value = sectionSettings["processingDirectory"]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74813 次 |
| 最近记录: |