我想用我的应用程序的配置保存设置2公司的,而且我preFER如果有可能使用一节单独的数据从另一个而不是给他们指出错误的键名
我一直在网上检查,但我似乎变得有点不知所措,当人们使用的部分或寻找过时的简单的方法来使用它们。任何人都可以通过我的初学者指南呢?
下面是什么我的app.config看起来像一个exampkle。
< configSections>
<节名称=FBITYPE =/>
<节名称=FSCS式=/>
< / configSections>
< FSCS>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FSCS>
< FBI>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FBI>
更新:
基于前面回答高级溶液。如果有人想知道的。
App.config中:
< FileCheckerConfigGroup>
< configSections>
< sectionGroup名=FileCheckerConfigGroup>
<节名称=FBITYPE =System.Configuration.NameValueSectionHandler/>
<节名称=FSCSTYPE =System.Configuration.NameValueSectionHandler/>
< / sectionGroup>
< / configSections>
< FileCheckerConfigGroup>
< FSCS>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FSCS>
< FBI>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FBI>
< / FileCheckerConfigGroup>
code:
//获取应用程序配置文件。
System.Configuration.Configuration配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//获取部组的集合。
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
的foreach(ConfigurationSectionGroup sectionGroup在sectionGroups)
{
如果(sectionGroup.Name ==FileCheckerConfigGroup)
{
的foreach(配置节的ConfigurationSection在sectionGroup.Sections)
{
VAR节= ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName)为NameValueCollection中;
inputDirectory =节[inputDirectory]; //C:\ testfiles;
}
}
}
解决方案
< configSections>
<节名称=FBITYPE =System.Configuration.NameValueSectionHandler/>
<节名称=FSCSTYPE =System.Configuration.NameValueSectionHandler/>
< / configSections>
< FSCS>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FSCS>
< FBI>
<添加键=processingDirectory值=C: testfiles ProccesFolder/>
< / FBI>
然后:
VAR部分= ConfigurationManager.GetSection(FSCS)作为NameValueCollection中;
VAR值=节[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 exampkle of what my app.config would look like.
<configSections>
<section name="FBI" type="" />
<section name="FSCS" type="" />
</configSections>
<FSCS>
<add key="processingDirectory" value="C:testfilesProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:testfilesProccesFolder"/>
</FBI>
Update:
Advanced solution based on the anwer. in case anyone wanted to know.
App.config:
<FileCheckerConfigGroup>
<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:testfilesProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:testfilesProccesFolder"/>
</FBI>
</FileCheckerConfigGroup>
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:testfilesProccesFolder"/>
</FSCS>
<FBI>
<add key="processingDirectory" value="C:testfilesProccesFolder"/>
</FBI>
And then:
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];