C # app.config file

this is my code ...

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    AppSettingsSection configSection = config.AppSettings;

    try
    {
        if (configSection != null)
        {
            if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false)
            {
                configSection.Settings["DailyFilName"].Value = "NewValue";
                config.Save();
            }
        }
    }
    catch (ConfigurationException ex)
    {
        MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

But it does not update my configuration file :(

+3
source share
4 answers

you should use this:

configSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);

take a look at the ConfigurationSaveMode enumeration for more options

+1
source

Here is the solution from the article How to change the running time of the App.config file using C #

 // Open App.Config of executable

 System.Configuration.Configuration config = 
 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 // Add an Application Setting.

 config.AppSettings.Settings.Remove("LastDateFeesChecked");

 config.AppSettings.Settings.Add("LastDateFeesChecked",    
 DateTime.Now.ToShortDateString());

 // Save the configuration file.

 config.Save(ConfigurationSaveMode.Modified);

 // Force a reload of a changed section.

ConfigurationManager.RefreshSection("appSettings");
+1
source

replace config.Save ();

to be

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
0
source

I think the problem may be that you do not have configuration options as "custom"

0
source

Source: https://habr.com/ru/post/1776419/


All Articles