如何重新排序配置文件中的AppSettings和Custom Section? [英] How to reorder AppSettings and Custom Section in Configuration file?

查看:72
本文介绍了如何重新排序配置文件中的AppSettings和Custom Section?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了如下的自定义配置文件。

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>

我想更改appSettings和deviceConfiguration的顺序如下。

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>

如何执行此操作?

我创建默认配置文件,但始终创建​​deviceConfiguration> appSettings order。

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

我想我应该把它设为appSettings> deviceConfiguration 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?

推荐答案

无法做到这一点。 配置文件专为ConfigurationManager独家使用而设计。 它将整个文档读入对象树,因此它不关心排序。 如果您打算在
上使用该文件,那么您必须适应ConfigurationManager的标准。

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?


这篇关于如何重新排序配置文件中的AppSettings和Custom Section?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆