none
How to reorder AppSettings and Custom Section in Configuration file? RRS feed

  • Question

  • I created custom configuration file like following.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="deviceConfiguration" type="................" allowLocation="true" allowDefinition="Everywhere" />
        </configSections>
        <deviceConfiguration>
            <deviceList>
                <device>
                </device>
            </deviceList>
        </deviceConfiguration>
        <appSettings>
            <add key="..........." value=".." />
            <add key="..........." value="...." />
        </appSettings>
    </configuration>

    I want to change the order of appSettings and deviceConfiguration like following.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="deviceConfiguration" type="................" allowLocation="true" allowDefinition="Everywhere" />
        </configSections>
        <appSettings>
            <add key="..........." value=".." />
            <add key="..........." value="...." />
        </appSettings>
        <deviceConfiguration>
            <deviceList>
                <device>
                </device>
            </deviceList>
        </deviceConfiguration>
    </configuration>

    How to do this?

    I create default configuration file but always created deviceConfiguration > appSettings order.

    I think I should make it appSettings > deviceConfiguration order but don't know how to do it.

    Here is the code snippet I create the Configuration file.

    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("...........", "..");
                    config.AppSettings.Settings.Add("...........", "....");
                    //SPMConfiguratorSection
                    DeviceConfiguration section1 = new DeviceConfiguration();
                    config.Sections.Add("deviceConfiguration", section1);
                    config.Sections["deviceConfiguration"].SectionInformation.AllowLocation = true;
                    config.Sections["deviceConfiguration"].SectionInformation.AllowDefinition = ConfigurationAllowDefinition.Everywhere; 
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("configuration");
                }
                catch (ConfigurationErrorsException ex)
                {
                    MessageBox.Show("Failed to create configuration file." + Environment.NewLine +
                                    ex.Message);
                    return false;
                }
                return true;
            }
    Can anybody give me any advice?

    Friday, December 28, 2018 8:41 PM

All replies

  • There is no way to do that.  The configuration file is designed for the exclusive use of the ConfigurationManager.  It reads the whole document into an object tree, so it doesn't care about the ordering.  If you intend to use that file on your own, then you must adapt to ConfigurationManager's standards.

    Why would this even matter to you?


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

    Sunday, December 30, 2018 8:13 AM
  • Hi Jeff0803,

    As far as I know that ConfigurationManager does not provide related method to reorder the items in configuration node.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 31, 2018 3:24 AM
    Moderator