Wednesday, May 14, 2008

Reading & Writing other application config files

I recently had to write a configuration tool to manage the configuration settings of a bunch of other programs. 
Here's a little snippet, which also shows using XPath to read XML.




using System.Xml.XPath;


private string ReadOtherAppConfigSetting(string fileName, string setting)
{
string result = "";

try
{
XmlDocument exeConfig = new XmlDocument();
exeConfig.Load(fileName);
XPathNavigator xNav = exeConfig.CreateNavigator();
XPathExpression dsExpr = xNav.Compile("//configuration/appsettings/add[@key='"+setting+"']");
XPathNodeIterator iterator = xNav.Select(dsExpr);

if (iterator.Count == 1)
{
if (iterator.CurrentPosition == 0)
{
iterator.MoveNext();
}

result = iterator.Current.GetAttribute("value", "");
}
}
catch {} //Don't care if can't find or doesn't exist as can default to empty string

return result;
}

private bool WriteOtherAppConfigSetting(string fileName, string setting, string newValue)
{
bool result = false;

XmlDocument exeConfig = new XmlDocument();
XmlNode oldNode = null;

if (File.Exists(fileName))
{
exeConfig.Load(fileName);

try
{
oldNode = exeConfig.DocumentElement.SelectSingleNode("//configuration/appsettings/add[@key='"+setting+"']");
}
catch {} //old child or file may not exist

//Delete existing entry (if exists)
if (oldNode != null)
{
exeConfig.LastChild.FirstChild.RemoveChild(oldNode);
}
}
else
{
//If file doesn't exist, create a basic structure
exeConfig.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration><appsettings></appsettings></configuration>");
}

try
{
//add the new setting
XmlElement newNode = exeConfig.CreateElement("add");
newNode.SetAttribute("key", setting);
newNode.SetAttribute("value", newValue);

exeConfig.LastChild.FirstChild. AppendChild(newNode);
exeConfig.Save(fileName);
result = true;
}
catch
{
throw new IOException("Unable to save configuration settings. Manually edit config file or delete it and then try resaving settings.");
}

return result;
}

1 comment:

  1. Usually you should not put on water-resistant wrist watches swimming whilst going for a finalizing rolex replica to avoid the actual breach associated with perspiration as well as dangerous smells, although not not at all drinking water. As the chanel replica throughout make use of is going to be a few unavoidable trend: every single day from the face filling device, manage mind or even manage from the very first pipe may progressively put on the actual silicone music group. Suffering from heat as well as the top of cup heat tag heuer replica as well as contraction, and also the situation can make this seem about the move; include the actual water-resistant attire or even getting older results associated with deterioration . through perspiration, that will trigger water desk over the actual water-resistant tag heuer replica. Therefore, the final drinking water desk should not put on to visit floating around, have a shower. May be the laundry washing, cleaning meals, cooking food period ought to take care not to little drinking water up for grabs or even water steam to the desk, tag heuer replica it might be rustic drinking water desk components, influencing the products the actual suggestion, as well as shed worth. At the moment any kind of among the water-resistant view, regardless of the number of yards tend to be designated along with anti-are not really within water (> 40C °) used.

    ReplyDelete