Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15702

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

$
0
0
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:

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);
            }
        }


Viewing all articles
Browse latest Browse all 15702

Trending Articles