c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

How to programmatically modifying the AppSetting value in web.config using c#?

| | ASP-NET , web

In this tutorial I will show you how to modify the AppSettings value in web.config file programmatically. Here, I am updating the app key value of petrol, diesel, oil prices and save the appsettings.you can create textboxs and update button. When the user want to change the price then entering the value in textboxes, on button click you can update the appsettings value.

Web.config Code:

 <appSettings>
  <add key="PetrolRate" value="112.53" />
  <add key="DieselRate" value="101.36" />
  <add key="OilRate" value="312" />
 </appSettings>









C# Code:

// to

open the configuration file
Configuration
webConfigApp = WebConfigurationManager.OpenWebConfiguration(
"~");
//
changing the app key value
webConfigApp.AppSettings.Settings["PetrolRate"].Value = "112.53";
webConfigApp.AppSettings.Settings["DieselRate"].Value = "101.36";
webConfigApp.AppSettings.Settings["OilRate"].Value = "312";
// save
the appsettings
webConfigApp.Save();