ConfigurationManager doesn't save settings

前端 未结 3 1932
孤街浪徒
孤街浪徒 2020-11-27 07:27

Here is the code I\'m using:

private void SaveConfiguration()
{
    if (txtUsername.Text != \"\" && txtPassword.Text != \"\")
    {
        Configura         


        
3条回答
  •  臣服心动
    2020-11-27 08:09

    I think you should call the Save method

    ConfigurationManager.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    

    EDIT

    To be able to save you have to use a configuration object returned by the OpenExeConfiguration Method

    //Create the object
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
    //make changes
    config.AppSettings.Settings["Username"].Value = txtUsername.Text;
    config.AppSettings.Settings["Password"].Value = txtPassword.Text;
    
    //save to apply changes
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    

    More references here ConfigurationManager Class

提交回复
热议问题