There is no reason why the winform write app.config file changed when debugging

  • 2020-05-09 19:10:31
  • OfStack

Environmental vs2010
ConfigurationManager.AppSettings [""].ToString ()
  doesn't write config very often. The code is as follows:

 


 /// <summary>
///  Modify the value of an item in the configuration file 
/// </summary>
/// <param name="key">appSettings the key</param>
/// <param name="value">appSettings the Value</param>
public static void SetConfig(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] != null)
config.AppSettings.Settings[key].Value = value;
else
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}


However, during debugging, I found that the contents of app.config under the project had not changed.   without debugging (between finding the debug file under the project) running the exe file found the app.config file changed.

The reason:

When we debug   (press F5), the compiler overwrites exe, dll, config under debug. Then execute the exe file and vshost.exe (for debugging). This will change the content of config under debug.
The app.config file under the project has not changed... So the next debugging time, config under debug was overwritten by app.config. So you see what I said above.


Related articles: