本文介绍了如何重新排序配置文件中的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?
推荐答案
为什么这对您来说很重要?
Why would this even matter to you?
这篇关于如何重新排序配置文件中的AppSettings和Custom Section?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!