How to get all sections of a specific type

Let's say I have the following in my configuration:

<configSections>
  <section name="interestingThings" type="Test.InterestingThingsSection, Test" />
  <section name="moreInterestingThings" type="Test.InterestingThingsSection, Test" />
</configSections>

<interestingThings>
  <add name="Thing1" value="Seuss" />
</interestingThings>

<moreInterestingThings>
  <add name="Thing2" value="Seuss" />
</moreInterestingThings>

      

If I want to get any section, I can get them easily by name:

InterestingThingsSection interesting = (InterestingThingsSection)ConfigurationManager.GetSection("interestingThings");
InterestingThingsSection more = (InterestingThingsSection)ConfigurationManager.GetSection("moreInterestingThings");

      

However, it depends on my code, knowing how the section is specified in the config - and it could be called anything. What I would prefer is the ability to pull all type sections InterestingThingsSection

from the config regardless of the name. How can I do this in a flexible way (so it supports both application configs and web configs)?

EDIT: If you already have it Configuration

, getting the actual sections isn't that hard:

public static IEnumerable<T> SectionsOfType<T>(this Configuration configuration)
    where T : ConfigurationSection
{
    return configuration.Sections.OfType<T>().Union(
        configuration.SectionGroups.SectionsOfType<T>());
}

public static IEnumerable<T> SectionsOfType<T>(this ConfigurationSectionGroupCollection collection)
    where T : ConfigurationSection
{
    var sections = new List<T>();
    foreach (ConfigurationSectionGroup group in collection)
    {
        sections.AddRange(group.Sections.OfType<T>());
        sections.AddRange(group.SectionGroups.SectionsOfType<T>());
    }
    return sections;
}

      

However, how do I get an instance Configuration

in common mode? Or how can I know if I should use ConfigurationManager

or WebConfigurationManager

?

+3


source to share


3 answers


So far this looks its best:



var config = HostingEnvironment.IsHosted
    ? WebConfigurationManager.OpenWebConfiguration(null) // Web app.
    : ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Desktop app.

      

+1


source


This may not be the best way to do it, but you can read your config file as normal xml and then parse the sections you want. For example, if this is a web application:

XmlDocument myConfig= new XmlDocument();
myConfig.Load(Server.MapPath("~/Web.config"));
XmlNode xnodes = myConfig.SelectSingleNode("/configSections");

      

Now you can see the nodes you need to detect names at runtime and then access the specific node of your config file.

Another solution:



Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

      

If this returns "web.config", it is probably a web application.

However, HostingEnvironment.IsHosted is meant to indicate if the appdomain is configured to run under ASP.NET, so it's not sure what your web application is.

0


source


Try using the method ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

. It opens the configuration file for the current application as an object Configuration

.

MSDN documentation: https://msdn.microsoft.com/en-us/library/ms134265%28v=vs.110%29.aspx

0


source







All Articles