Programatically Modifying and Saving AppSetting value in Root Level Web.Config using ASP.NET C#

In this blog we are going to see, How to Modify the AppSettings value and Save it.

 Root Level Web.Config

  <appSettings>
    <
add key="AppKey" value="AppValue"/>
  </appSettings>

Code Snippet

using System;
using System.Configuration;
using System.Web.Configuration;

namespace SampleApplication.WebConfig
{
    public partial class webConfigFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)

        {
            //Helps to open the Root level web.config file.
            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");

            //Modifying the AppKey from AppValue to AppValue1
            webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";

            //Save the Modified settings of AppSettings.
            webConfigApp.Save();
        }
    }
}

Output of Root Level Web.Config:

 <appSettings>
    <
add key="AppKey" value="AppValue1"/>
  </appSettings>