This blog is moved to
http://amalhashim.wordpress.com

Monday, May 11, 2009

Runtime app.config modification

static void Main(string[] args)
{
UpdateAppSettings("abc", "value1000");
ConfigurationManager.RefreshSection("appsettings");
Console.WriteLine(ConfigurationManager.AppSettings["abc"]);
}

static void UpdateAppSettings(String KeyName, String KeyValue)
{
XmlDocument XmlDoc = new XmlDocument();

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

foreach(XmlElement xElement in XmlDoc.DocumentElement)
{
if(xElement.Name == "appSettings")
{
foreach(XmlNode xNode in xElement.ChildNodes)
{
if(xNode.Attributes[0].Value == KeyName)
{
xNode.Attributes[1].Value = KeyValue;
}
}
}

}

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

}

No comments: