Add,Delete config key dynamically in asp.net

First of all you need to start visual studio as admin.

Put this code on add button click :

 protected void btnAdd_Click(object sender, EventArgs e)
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);        
        //Configuration config = WebConfigurationManager.OpenWebConfiguration("~");        
        config.AppSettings.Settings.Add(txtKey.Text, txtValue.Text);       
        config.Save();
        msgLabel.Text = "key added in config file";
        txtKey.Text = null;
        txtValue.Text = null;
    }

Put this code on delete button after entering key name in textbox.

 protected void btnDelete_Click(object sender, EventArgs e)
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        config.AppSettings.Settings.Remove(txtKey.Text);
        config.Save();
        msgLabel.Text = "key deleted from config file";
        txtKey.Text = null;
    }
Ebook Download
View all
Learn
View all