Change the value in app.config file dynamically

前端 未结 6 864
孤城傲影
孤城傲影 2020-11-27 15:01

I want to modify a value in appSetting section in app.config. So i wrote,

Console.WriteLine(ConfigurationManager.AppSettings[\"name\"]);
Console.Read();
Conf         


        
6条回答
  •  离开以前
    2020-11-27 15:37

    You have to update your app.config file manually

    // Load the app.config file
    XmlDocument xml = new XmlDocument();
    xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    
    // Do whatever you need, like modifying the appSettings section
    
    // Save the new setting
    xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    

    And then tell your application to reload any section you modified

    ConfigurationManager.RefreshSection("appSettings");
    

提交回复
热议问题