Dong Lam Trien

Dong Lam Trien

  • 778
  • 968
  • 133.9k

How do I update the Keys in the app.config file at runtime ?

Jun 17 2021 4:24 AM

I found some code examples that update the keys in the app.config file when running without error and not updating, why the code does not update, see my code 

file app.config
[CODE]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <appSettings>    
    <add key="FormatImage" value="Jpg" />        
    <add key="LogFile" value="C:\Windows\Log" />    
        
  </appSettings>    
  
</configuration>
[/CODE]

[CODE]
private void cmdUpdate_Click(object sender, EventArgs e)
        {
            EditAppSetting("LogFile", "D:\Office2010");
        }


public static void EditAppSetting(string key, string value)
        {
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[key].Value = value;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {               
                MessageBox.Show("Error update file App.config: " + ex.Message, "Thông báo ki?m tra !", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }
[/CODE]


Answers (1)