问题描述
我想使用我的应用程序配置来存储 2 家公司的设置,我更喜欢是否可以使用一个部分将一个公司的数据与另一个公司分开,而不是给他们不同的键名.
>我一直在网上查看,但当人们使用部分或找到过时的简单使用方法时,我似乎有点不知所措.谁能给我一份初学者指南?
下面是我的 app.config 的示例:
<section name="FBI" type=""/><section name="FSCS" type=""/></configSections><FSCS><add key="processingDirectory" value="C:\testfiles\ProccesFolder"/></FSCS><联邦调查局><add key="processingDirectory" value="C:\testfiles\ProccesFolder"/></FBI>
更新:
基于答案的高级解决方案.以防万一有人想知道.
App.config:
<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><联邦调查局><add key="processingDirectory" value="C:\testfiles\ProccesFolder"/></FBI></FileCheckerConfigGroup></配置>代码:
//获取应用程序配置文件.System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//获取节组的集合.ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;foreach (ConfigurationSectionGroup sectionGroup in sectionGroups){if (sectionGroup.Name == "FileCheckerConfigGroup"){foreach(SectionGroup.Sections 中的ConfigurationSection configurationSection){var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;inputDirectory = section["inputDirectory"];//"C:\\testfiles";}}}
<section name="FBI" type="System.Configuration.NameValueSectionHandler"/><section name="FSCS" type="System.Configuration.NameValueSectionHandler"/></configSections><FSCS><add key="processingDirectory" value="C:\testfiles\ProccesFolder"/></FSCS><联邦调查局><add key="processingDirectory" value="C:\testfiles\ProccesFolder"/></FBI>
然后:
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;var value = section["processingDirectory"];
I want to use my app config to store the settings for 2 companys, and i'd prefer if it was possible to use a section to seperate the data for one from the other rather then giving them diffrent key names.
I have been checking online but i seem to get a bit overwhelmed when people use sections or find outdated easy ways to use them. could anyone pass me a beginner guide on them?
Below is an example of what my app.config would look like:
<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>
Update:
Advanced solution based on the anwer. in case anyone wanted to know.
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>
Code:
// 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";
}
}
}
<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>
And then:
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
这篇关于你如何使用 c# 4.0 app.config 中的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!