How to reorder AppSettings and Custom Section in Configuration file?

  • Thread starter Thread starter Jeff0803
  • Start date Start date
J

Jeff0803

Guest
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?

Continue reading...
 
Back
Top