Search code examples
c#configurationmanager

How do I update .NET configuration in a custom config section?


We have an application that has a exe.config file much like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <configSections>
        <sectionGroup name="ConsoleSettings">
        <section name="Console" type="MyApp.ConfigSection" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>    
    </configSections>

    <ConsoleSettings>
        <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" />
    </ConsoleSettings>
....

What I would like to do is read the file, change the LanAddress to something the user entered (say, string newLanAddress) and then save it back.

So far, I have this:

var configFile = new ExeConfigurationFileMap();
var configFile.ExeConfigFilename = "MyApp.exe.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);

var configGroup = config.SectionGroups[@"ConsoleSettings"];
var consoleSection = configGroup.Sections[0];
var lanAddress = consoleSection.// this is where I get stuck

How do I access the LanAddress element of consoleSection??


Solution

  • We can create custom configuration section class.

    public class ConsoleSection : ConfigurationSection
    {
        [ConfigurationProperty("Username", IsRequired = true)]
        public string Username
        {
            get
            {
                return (string)this["Username"];
            }
            set
            {
                this["Username"] = value;
            }
        }
    
        [ConfigurationProperty("Password", IsRequired = true)]
        public String Password
        {
            get
            {
                return (String)this["Password"];
            }
            set
            {
                this["Password"] = value;
            }
        }
    
        [ConfigurationProperty("LanAddress", IsRequired = true)]
        public string LanAddress
        {
            get
            {
                return (string)this["LanAddress"];
            }
            set
            {
                this["LanAddress"] = value;
            }
        }
    
        [ConfigurationProperty("Port", IsRequired = false)]
        [IntegerValidator(ExcludeRange = false, MaxValue = short.MaxValue, MinValue = short.MinValue)]
        public int Port
        {
            get
            {
                return (int)this["Port"];
            }
            set
            {
                this["Port"] = value;
            }
        }
    }
    

    To read the config section we should do the following.

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var consoleSection = (ConsoleSection)config.GetSection("ConsoleSettings/Console");
    System.Console.WriteLine("ip: {0}", consoleSection.LanAddress);
    

    App.config is very similar to your one.

    <configSections>
      <sectionGroup name="ConsoleSettings">
        <section name="Console" type="MyApp.ConsoleSection, MyApp" allowLocation="true" allowDefinition="Everywhere" />
      </sectionGroup>
    </configSections>
    <ConsoleSettings>
      <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" />
    </ConsoleSettings>