How To Update Values In Web/App Config During Runtime In C#

In this article, we are going to display all values of appSettings in the Web.config file. and also update it during runtime.

We need to import/use the following libraries for reading and writing data of the config file.

  1. System.Configuration
  2. System.Xml

we are going to use ConfigurationManager to Get/update the content of the Web/application configuration file.

Web.config file

Sample Web.config file:

<configuration>
  <appSettings>
    
    <add key="Name" value="tabish" />
    <add key="MobileNo" value="8866141791" />
    <add key="Email" value="tabishzrangrej.vision@gmail.com" />
  
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  
  </appSettings>
</configuration>

 

Get/Print all Key-Value

in this article, I also demonstrate how to display all key and value of appSettings. if you want to display/show all key and value then you can use the following code.

public ActionResult Index()
{
    string OutputStr = "";
    foreach (string key in ConfigurationManager.AppSettings)
    {
    string value = ConfigurationManager.AppSettings[key];
    OutputStr += String.Format("Key: {0}, Value: {1} {2}", key, value, "\n");
    }
    ViewBag.AppConfigValues = OutputStr;
    return View();
}

above code will retrieve all the Key-Value of appSettings from Web.config file.

For read access, we do not need to use/call the OpenExeConfiguration.

you can display it on your view page if you want to like this.

@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h2>appSettings Details</h2>
    <pre class="lead">@ViewBag.AppConfigValues</pre>
</div>

above Index.cshtml file will display all Key-Value.

Edit/Update an existing key’s value

for the demonstration, we are going to update the Email. you can update whatever you want using the key. please refer to the following code.

public ActionResult UpdateEmail()
{
    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    foreach (XmlElement element in xmlDoc.DocumentElement)
    {
    if (element.Name.Equals("appSettings"))
    {
      foreach (XmlNode node in element.ChildNodes)
      {
        if (node.Attributes[0].Value.Equals("Email"))
        {
          node.Attributes[1].Value = "tabishzrangrej.vision@gmail.com";
        }
      }
    }
    }

    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    ConfigurationManager.RefreshSection("appSettings");

    return RedirectToAction("Index");
}

above code will change the value of Email. you can give whatever key for which value you want to update.

it will update the value of a particular key during runtime.

we used XmlDocument Because of the property of Configuration.AppSettings is read-only. if we want to modify the current application settings value then we need to directly update the application configuration file as an XML document using XmlDocument Class.

that’s it.this way we can get and update values in Web/App.config file during runtime.

Submit a Comment

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

Subscribe

Select Categories