c# .net

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

How to programmatically modifying the AppSetting value in web.config using c#?, someone asked me to explain?

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();



Post your comments / questions