1

I would like to create a section in my web.config file like this:

<paths>
                <path>\\123.123.132.123\c$\test\folder</path>
                <path>\\123.123.132.123\c$\test\folder</path>
</paths>

I am searching for alternatives, I would like to use one of the default section handlers, but I could only find section handlers that would work with this config

<CustomGroup>
        <add key="key1" value="value1"/>
</CustomGroup>

(that would be SingleTagSectionHandlers, DictionarySectionHandlers, NameValueSectionHandler and so on).

Is there any way that I substitute the < add> tag for a < path> tag? Or do I have to implement the IConfigurationSectionHandler interface?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
JSBach
  • 4,679
  • 8
  • 51
  • 98
  • I didn't find an alternative when I had to do this. But it's really not that difficult to implement. – Jim Mischel Sep 04 '13 at 19:24
  • Yes, it is not difficult, but I would rather not have this new class in the project, but I thinks this is the way to do it – JSBach Sep 04 '13 at 19:45

1 Answers1

2

do I have to implement the IConfigurationSectionHandler interface?

You don't have to if you use the System.Configuration.IgnoreSectionHandler.

web.config

<configuration>
  <configSections>
    <section name="Paths" type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
  <Paths>
    <path>\\123.123.132.123\c$\test\folder</path>
    <path>\\123.123.132.123\c$\test\folder</path>
  </Paths>

Then you can manually read the web.config with whatever you want to get your values.

public IEnumerable<string> GetPathsFromConfig()
{
  var xdoc = XDocument.Load(ConfigurationManager
    .OpenExeConfiguration(ConfigurationUserLevel.None)
    .FilePath);

  var paths = xdoc.Descendants("Paths")
    .Descendants("path")
    .Select(x => x.Value);

  return paths
}

Other wise you'll have to Create Custom Configuration Sections Using ConfigurationSection (how-to).

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Yes, I will create the custom config section then :) tks – JSBach Sep 04 '13 at 19:46
  • 1
    @Oscar FYI you can't create section with elements containing text - configuration elements supposed to map properties to attributes. So, for your case it's better to ignore section and parse it manually. – Sergey Berezovskiy Sep 04 '13 at 19:48
  • You are right, just noticed that. I changed the layout for :) thanks for the info! – JSBach Sep 04 '13 at 22:26