How to Modify web.config appsettings at runtime in c#?

The file web.config is responsible for the configuration of the project and also an important part. Through this post I will tell you how to modify web.config programmatically in c# at runtime and I also tell you about good and bad effects of doing this.

How to Modify web.config appsettings at runtime in c#

Before that lets talk about the web.config file and what things this fill does.

What is web.config?

web.config is defined as the main configuration file including settings for an ASP.NET web application. Basically this file resides in Web Configuration of the application and coded in XML document and contains the settings of how the site or application will work.

This file contains code that controls different activities like security configuration, module loading, session state configuration and more. This file also contains application items for example connection string to connect database.

How to Modify web.config appsettings in c#?

Appsettings is the section in web.config file where programmer can set his/her own application related settings like connection strings etc.  Programmer can set his/her own required settings in appsettings by adding Key and Value fields.

  1. Key: Here programmer put the name of the settings through which he can access the value.
  2. Value: Here you can put value(real value) that you want to access in different parts of application.

Well you can update this file during development and can set all required settings like this:

[sourcecode language=”xml”]

<appSettings>

<!–Connection String–>

<add key="ConnectionString" value="Data Source=testIP;Initial Catalog=dbo;User ID=test;Password=password;" />

</appSettings>

[/sourcecode]

Now to access this connection string just write the following code:

[sourcecode language=”plain”]

ConfigurationManager.AppSettings["ConnectionString"];

[/sourcecode]

You also required to add the following namespace in order to do the upper code.

[sourcecode language=”plain”]

using System.Configuration;

[/sourcecode]

How to Modify web.config appsettings at runtime in c#?

Now many developers required to update or modify appsettings section in web.config at run time to do dynamic operations because sometime it is required to do the activity whose values are only available during run time.

So, how you can modify web.config’s appsettings section during time?

Check out the following code:

[sourcecode language=”plain”]

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["ConnectionString"].Value = "ConnectionString";

//Save the Modified settings of AppSettings.

webConfigApp.Save();

}

}

}

[/sourcecode]

This code will allow you to update any key in appsettings during live and you can perform dynamic functions easily. This is the way how to modify web.config appsettings at runtim in c#.

For more stay connected to www.codealumni.com

Leave a Reply

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