Thursday, November 29, 2012

C# Windows Application Update app.config file

In the Post C# Windows Application Reading app.config file, we saw on how to read the key values in the App.config file, there are situations where we will have to update the values in the app.config file dynamically from within the application. 

In this post we shall see on how to update the values in the app.config file from the application.

App.config file before the update.

<?xml version="1.0" encoding="utf-8" ?>
    <add key="TestKey" value="OriginalTestValue" />


Code to Update the app.config file

XmlDoc = new XmlDocument();
XmlNode keyNode = XmlDoc.SelectSingleNode("/configuration/appSettings/add");
keyNode.Attributes[1].Value = "UpdatedTestValue";


App.config file after the update.

<?xml version="1.0" encoding="utf-8"?>
    <add key="TestKey" value="UpdatedTestValue" />

Add a reference to the System.Xml Namespace, since we are parsing the app.Config file.
using System.Xml;

Search Flipkart Products:

No comments: