3

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 :(

3
  • 3
    We are not compilers to read your code and understand how it will compile/run. Little more explanation would help you and us ;-) Commented Nov 23, 2010 at 10:15
  • what is the scope of the configuration values? Commented Nov 23, 2010 at 10:16
  • 3
    Exception or some more info would be helpful indeed. Wildguess, did you misspell 'DailyFilName'? Commented Nov 23, 2010 at 10:16

4 Answers 4

1

you should use this :

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

take a look at the ConfigurationSaveMode enum for more options

Sign up to request clarification or add additional context in comments.

1 Comment

also, just noticed the name of the settings is not written correctly "DailyFilName". perhaps a typo here?
1

Here is a solution from the article How to change App.config file run time 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 Comment

Verbatim unattributed copy from chiragrdarji.wordpress.com/2008/09/25/…
0

replace config.Save();

to be

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

Comments

0

I think the problem could be you don't have your configuration parameters as "user-scoped"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.