I found this code on
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx?display=Print that
loops through all section groups and sections in app.config. The only problem
that I'm having is group.IsDeclared is always false. Also, the group names
and section names are not returned in the order in which they are declared in
the config file, which is not a big deal as long as all of them are being
returned, I was just wondering why that's the case.
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the collection of the section groups.
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
// Show the configuration values
ShowSectionGroupCollectionInfo(sectionGroups);
static void ShowSectionGroupCollectionInfo(
ConfigurationSectionGroupCollection sectionGroups)
{
ClientSettingsSection clientSection;
SettingValueElement value;
foreach(ConfigurationSectionGroup group in sectionGroups)
// Loop over all groups
{
if(!group.IsDeclared)
// Only the ones which are actually defined in app.config
continue;
Console.WriteLine("Group {0}", group.Name);
// get all sections inside group
foreach(ConfigurationSection section in group.Sections)
{
clientSection = section as ClientSettingsSection;
Console.WriteLine("\tSection: {0}", section);
if(clientSection == null)
continue;
foreach(SettingElement set in clientSection.Settings)
{
value = set.Value as SettingValueElement;
// print out value of each section
Console.WriteLine("\t\t{0}: {1}",
set.Name,value.ValueXml.InnerText);
}
}
}
Eve said:
This is the extract from my app.config file:
<configSections>
<section name="1"
type="System.Configuration.DictionarySectionHandler" />
<section name="2"
type="System.Configuration.DictionarySectionHandler" />
<section name="3"
type="System.Configuration.DictionarySectionHandler" />
</configSections>
<1>
<add key="DayOrMonth" value="M" />
<add key="NoOfDaysOrMonths" value="3" />
</1>
<2>
<add key="DayOrMonth" value="M" />
<add key="NoOfDaysOrMonths" value="2" />
</2>
<3>
<add key="DayOrMonth" value="D" />
<add key="NoOfDaysOrMonths" value="14" />
</3>
GetSectionSettings() code that I posted DOES WORK for me. I simply perform
this call:
GetSectionSettings("1");
GetSectionSettings("2");
GetSectionSettings("3");
and the method gets values for each element in each section (M and 3 for
section "1", M and 2 for section "2", D and 14 for section "3") and does some
processing based on those values.
I don't want to hard-code "1", "2", "3" when I call GetSectionSettings
because there could be 50 sections, not just 3, and I don't need my program
to know what the names of those sections are, I want the program to figure
that out by looping through <configSections> elements. It shouldn't be even
necessary for me to post GetSectionSettings() code because it's got NOTHING
to do with how I would loop through <configSections> elements.
Peter Duniho said:
Again, this is my code for GetSectionSettings method that accepts a
section
name as a parameter and retrieves the section's settings:
public void GetSectionSettings(string sectionName)
{
IDictionary configTable = (IDictionary)ConfigurationManager.GetSection
(sectionName);
string dayOrMonth = configTable["DayOrMonth"].ToString();
...
}
I don't see any way for that method to work generally. It can only work
for sections that implement the IDictionary interface, and you haven't
explained why the containing section does, never mind what type is
returned by that section.
I DON'T have any code that loops through <section> elements within
<configSections> because I HAVE NO CLUE HOW TO DO IT - that's why I
posted my
question on this forum.
I'm not asking for the code that you can't write. But you haven't even
posted a concise-but-complete code sample of what _does_ work.
Please see
http://www.yoda.arachsys.com/csharp/complete.html and
http://www.yoda.arachsys.com/csharp/incomplete.html