Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# app.config file

Tags:

c#

app-config

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 its not update my config file :(

like image 948
Charitha Avatar asked Apr 18 '26 03:04

Charitha


1 Answers

you should use this :

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

take a look at the ConfigurationSaveMode enum for more options

like image 158
Daniel Perez Avatar answered Apr 19 '26 16:04

Daniel Perez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!