Update Web Config File Programmatically

Hello guys, I will explain how we can update and save the “web.config” file programmatically in this article. So let’s start it.

Namespace:

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

Code:

// To open web.config file
Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");

//Update the key
webConfigApp.AppSettings.Settings["Your_Key"].Value = "New_Key_Value";

//Save changes
webConfigApp.Save();

//Refresh web.config file
ConfigurationManager.RefreshSection("appSettings");

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories