Home > other >  C# Config order AppSettings before saving
C# Config order AppSettings before saving

Time:10-15

Is there a way to order the AppSettings by key name before saving the file?

What I've tried:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
UpdateSetting(config, "LocalLastUpdate", remoteSettingLastUpdated);
config.AppSettings.Settings.AllKeys.OrderBy(r => r);  //this will not work b/c it's just the keys
config.Save(ConfigurationSaveMode.Minimal);


private static void UpdateSetting(Configuration config, string name, string value)
{
    Log.Debug("Updating Local Settings ["   name   "] -> '"   value   "'");
    config.AppSettings.Settings.Remove(name);
    config.AppSettings.Settings.Add(name, value);
}

Example:

<appSettings>
   <add key="LocalSettingsLastUpdate" value="9/30/2021 11:27:13 PM" />
   <add key="Printer_DefaultLogo" value="Y" />
   <add key="Signature_Width" value="300" />
</appSettings>

After I run code to add a setting to the config and hit save I get this:

<appSettings>
   <add key="LocalSettingsLastUpdate" value="9/30/2021 11:27:13 PM" />
   <add key="Printer_DefaultLogo" value="Y" />
   <add key="Signature_Width" value="300" />
   <add key="LocalLastUpdate" value="10/1/2021 11:27:14 PM" />  <- this should be at the top
</appSettings> 

CodePudding user response:

This code was tested in Visual Studio and works properly

public void AddUpdateSortAppSettings(List<(string, string)> newSettings)
 {
 var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

             var appSettings = configFile.AppSettings;

            if (appSettings.Settings.Count == 0) return ;  //error

            var settings = new List<(string, string)>();
            foreach (var key in appSettings.Settings.AllKeys)
            {
                settings.Add((appSettings.Settings[key].Key, appSettings.Settings[key].Value));
                appSettings.Settings.Remove(key);
            }

            for (var i = 0; i < newSettings.Count; i  )
            {
                var found = false;
                for (var j = 0; j < settings.Count; j  )
                {
                    if (settings[j].Item1 == newSettings[i].Item1) 
                     {
                     settings[j] = newSettings[i];
                    found = true;
                     }
                }
                if (!found) settings.Add(newSettings[i]);
            }

            var existingSettings = settings.OrderBy(i => i.Item1);

            foreach (var item in existingSettings)
            {
                appSettings.Settings.Add(item.Item1, item.Item2);
            }

            configFile.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}

to use it

var newSettings= new List<(string, string)>{("LocalLastUpdate","10/1/2022 11:27:14 PM")};
AddUpdateSortAppSettings(newSettings);
  • Related