none
How to create custom configsection? RRS feed

  • Question

  • I want to create Configuration file when it does not exist.

    Here is the default configuration file.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings>
            <add key="Code" value="AB" />
            <add key="Port" value="1000" />
            <add key="MachineID" value="M0001" />
        </appSettings>
    </configuration>

    And here is the code generate it.

    public bool CreateDefaultConfigFile(string configfile)
    {
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
        configFileMap.ExeConfigFilename = configfile;
        try
        {
            // OpenApp.Config of executable
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            // Add an Application Setting if not exist
            config.AppSettings.Settings.Add("Code", "AB");
            config.AppSettings.Settings.Add("Port", "1000");
            config.AppSettings.Settings.Add("MachineID", "M0001");
            config.Save(ConfigurationSaveMode.Modified);
        }
        catch(ConfigurationErrorsException ex)
        {
            MessageBox.Show("Failed to create configuration file." + Environment.NewLine + 
                            ex.Message);
            return false;
        }
        return true;
    }

    However I want to add custom ConfigSection like following(Bold/Italic).

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name = "printer" type = "MyPrinterType"/>
            <section name = "scanner" type = "MyScannerType"/>
        </configSections>
        <appSettings>
            <add key="Code" value="AB" />
            <add key="Port" value="1000" />
            <add key="MachineID" value="M0001" />
        </appSettings>
        <scanner>
            <deviceList>
            </deviceList>
        </scanner>
    </configuration>
    What should I add codes to the existing code?


    • Edited by Jeff0803 Wednesday, December 26, 2018 9:18 PM
    Wednesday, December 26, 2018 9:11 PM

Answers

  • The examples in the documentation show this pretty well.  You'll need to define your own classes to describe and hold your custom attributes, as shown on the ConfigurationSection page.  Then, you just create those custom sections and add them to your config as shown on the OpenMappedExeConfiguration page.

    public class Program
    {
        public sealed class MySection : ConfigurationSection {
            private static ConfigurationPropertyCollection _Properties;
    
            private static readonly ConfigurationProperty _DeviceName =
                new ConfigurationProperty("deviceName",
                    typeof(string),"none",
                    ConfigurationPropertyOptions.IsRequired);
    
            private static readonly ConfigurationProperty _FriendlyName =
                new ConfigurationProperty("friendlyName",
                    typeof(string),"none",
                    ConfigurationPropertyOptions.IsRequired);
    
    
            protected override ConfigurationPropertyCollection Properties
            {
                get { return _Properties; }
            }
            
            public string DeviceName
            {
                get { return (string)this["deviceName"]; }
                set { this["deviceName"] = value; }
            }
            
            public string FriendlyName
            {
                get { return (string)this["friendlyName"]; }
                set { this["friendlyName"] = value; }
            }
    
            public MySection()
            {
                _Properties = new ConfigurationPropertyCollection();
                _Properties.Add( _DeviceName );
                _Properties.Add( _FriendlyName );
            }
        };
    
        static void Main(string[] args)
        {
            ExeConfigurationFileMap cfmap = new ExeConfigurationFileMap();
            cfmap.ExeConfigFilename = "xxx.xml";
    
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
                cfmap, ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add("Code", "AB");
    
            MySection my1 = new MySection();
            config.Sections.Add( "scanner", my1 );
            MySection my2 = new MySection();
            config.Sections.Add( "printer", my2 );
            config.Save(ConfigurationSaveMode.Modified);
    
            ConfigurationManager.RefreshSection( "scanner" );
            ConfigurationManager.RefreshSection( "printer" );
            my1.DeviceName = "device 1";
            my1.FriendlyName = "friendly 1";
            my2.DeviceName = "device 2";
            my2.FriendlyName = "friendly 2";
            config.Save(ConfigurationSaveMode.Modified);
        }
    }
    
    
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Jeff0803 Friday, December 28, 2018 8:42 PM
    Thursday, December 27, 2018 1:44 AM

All replies

  • The examples in the documentation show this pretty well.  You'll need to define your own classes to describe and hold your custom attributes, as shown on the ConfigurationSection page.  Then, you just create those custom sections and add them to your config as shown on the OpenMappedExeConfiguration page.

    public class Program
    {
        public sealed class MySection : ConfigurationSection {
            private static ConfigurationPropertyCollection _Properties;
    
            private static readonly ConfigurationProperty _DeviceName =
                new ConfigurationProperty("deviceName",
                    typeof(string),"none",
                    ConfigurationPropertyOptions.IsRequired);
    
            private static readonly ConfigurationProperty _FriendlyName =
                new ConfigurationProperty("friendlyName",
                    typeof(string),"none",
                    ConfigurationPropertyOptions.IsRequired);
    
    
            protected override ConfigurationPropertyCollection Properties
            {
                get { return _Properties; }
            }
            
            public string DeviceName
            {
                get { return (string)this["deviceName"]; }
                set { this["deviceName"] = value; }
            }
            
            public string FriendlyName
            {
                get { return (string)this["friendlyName"]; }
                set { this["friendlyName"] = value; }
            }
    
            public MySection()
            {
                _Properties = new ConfigurationPropertyCollection();
                _Properties.Add( _DeviceName );
                _Properties.Add( _FriendlyName );
            }
        };
    
        static void Main(string[] args)
        {
            ExeConfigurationFileMap cfmap = new ExeConfigurationFileMap();
            cfmap.ExeConfigFilename = "xxx.xml";
    
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
                cfmap, ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add("Code", "AB");
    
            MySection my1 = new MySection();
            config.Sections.Add( "scanner", my1 );
            MySection my2 = new MySection();
            config.Sections.Add( "printer", my2 );
            config.Save(ConfigurationSaveMode.Modified);
    
            ConfigurationManager.RefreshSection( "scanner" );
            ConfigurationManager.RefreshSection( "printer" );
            my1.DeviceName = "device 1";
            my1.FriendlyName = "friendly 1";
            my2.DeviceName = "device 2";
            my2.FriendlyName = "friendly 2";
            config.Save(ConfigurationSaveMode.Modified);
        }
    }
    
    
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Jeff0803 Friday, December 28, 2018 8:42 PM
    Thursday, December 27, 2018 1:44 AM
  • Can I change the order of "scanner" and "printer"?

    Even though I changed the order, the result configuration file is the same.

    Friday, December 28, 2018 8:49 PM