ConfigurationManager.AppSettings - How to modify and save?

后端 未结 9 1527
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 05:10

It might sound too trival to ask and I do the same thing as suggested in articles, yet it doesn\'t work as expected. Hope someone can point me to the right direction.

<
9条回答
  •  情书的邮戳
    2020-11-28 06:00

    as the base question is about win forms here is the solution : ( I just changed the code by user1032413 to rflect windowsForms settings ) if it's a new key :

    Configuration config = configurationManager.OpenExeConfiguration(Application.ExecutablePath); 
    config.AppSettings.Settings.Add("Key","Value");
    config.Save(ConfigurationSaveMode.Modified);
    

    if the key already exists :

    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
    config.AppSettings.Settings["Key"].Value="Value";
    config.Save(ConfigurationSaveMode.Modified);
    

提交回复
热议问题