Change the value in app.config file dynamically

前端 未结 6 867
孤城傲影
孤城傲影 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:42

    This code works for me:

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
        config.AppSettings.Settings["test"].Value = "blah";       
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    

    Note: it doesn't update the solution item 'app.config', but the '.exe.config' one in the bin/ folder if you run it with F5.

提交回复
热议问题