Read and Write AppSettings value from ASP.NET web.config File Example

Updated on     Kisan Patel

This tutorial will explain you how to read and write AppSettings value from ASP.NET web.config file.

The <appSettings> element stores custom application configuration settings.

<appSettings>
   <add key="KeyName" value="KeyValue" />
</appSettings>

The key/value pairs specified in the <appSettings> element can be accessed in code using the System.Configuration.ConfigurationSettings class.

string value = WebConfigurationManager.AppSettings["KeyName"];

To modify Key Value, first you need to obtain configuration settings for the root-level using OpenWebConfiguration method. To update a configuration setting, use the Save or SaveAs method of the configuration object.

//Helps to open the Root level web.config file.
Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
//Modifying the AppKey from AppValue to AppValue1
webConfigApp.AppSettings.Settings["KeyName"].Value = "KeyValue";
//Save the Modified settings of AppSettings.
webConfigApp.Save();

Lets take example to understand how to read and write <appSettings>.

First Defined AppSettings Section in Web.Config file as shown below code:

web.config

<configuration>
   ...
   <appSettings>
      <add key="BackgroundColor" value="Red"/>
   </appSettings>
</configuration>

Now, add below line of code to Default.aspx page. here we have added one txt_bgColor TextBox and btn_save button. In this demo, we change the background color of the page by adding or changing bgcolor attribute value of the tag programmatically.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppSettingDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body id="PageBody" runat="server">
   <form id="form1" runat="server">
     <div>
         <asp:Label ID="lbl_bgColor" runat="server" Text="Color Name: "></asp:Label>
         <asp:TextBox ID="txt_bgColor" runat="server"></asp:TextBox>
     </div>
     <asp:Button ID="btn_Save" runat="server" Text="Change Color" OnClick="btn_Save_Click" />
   </form>
</body>
</html>

Now, add below line of code to Default.aspx.cs page

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

namespace WebAppSettingDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) 
            {
                PageBody.Attributes.Add("bgcolor", WebConfigurationManager.AppSettings["BackgroundColor"]);
            }
        }

        protected void btn_Save_Click(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["BackgroundColor"].Value = txt_bgColor.Text;
            //Save the Modified settings of AppSettings.
            webConfigApp.Save();

            Response.Redirect(Request.RawUrl);
        }
    }
}

Demo

appSettings-demo

Download Complete Source Code


ASP.NET

Leave a Reply