Ik wil mijn app-configuratie gebruiken om de instellingen voor 2 bedrijven op te slaan, en ik zou liever hebben dat het mogelijk was om een sectie te gebruiken om de gegevens voor de een van de ander te scheiden in plaats van ze verschillende sleutelnamen te geven.
p>
Ik heb online gekeken, maar ik lijk een beetje overweldigd te raken als mensen secties gebruiken of verouderde eenvoudige manieren vinden om ze te gebruiken. kan iemand me een beginnersgids over hen geven?
Hieronder ziet u een voorbeeld van hoe mijn app.config eruit zou zien:
<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>
Bijwerken:
Geavanceerde oplossing op basis van het antwoord. voor het geval iemand het wilde weten.
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";
}
}
}
Antwoord 1, autoriteit 100%
<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>
En dan:
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
Antwoord 2, autoriteit 16%
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>
Voorbeeld van gebruik
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"]
}