c# - Traverse .config file in exe or web app -
i'm writing library used in desktop , web apps. needs traverse section groups in .config file. assume i'll need instance of system.configuration.configuration
this. there way works in desktop , web apps?
i ended this:
static configuration openconfiguration() { if (httpcontext.current != null) return webconfigurationmanager.openwebconfiguration(null); return configurationmanager.openexeconfiguration(configurationuserlevel.none); } static ienumerable<configurationsectiongroup> getconfigsectiongroups() { var config = openconfiguration(); var stack = new stack<configurationsectiongroup>(); stack.push(config.rootsectiongroup); while (stack.count > 0) { var group = stack.pop(); yield return group; foreach (configurationsectiongroup subgroup in group.sectiongroups) { stack.push(subgroup); } } }
Comments
Post a Comment