h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

App Config File in C#.net

Read Setting Method
public static string ReadSetting(string key)
{
return ConfigurationSettings.AppSettings[key];
}
Write Setting Method
public static void WriteSetting(string key, string value)
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
try
{
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
if (elem != null)
{
elem.SetAttribute("value", value);
}
else
{
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(GetConfigFilePath());
}
catch
{
throw;
}
}
Remove Setting Method
public static void RemoveSetting(string key)
{
XmlDocument doc = LoadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
doc.Save(GetConfigFilePath());
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}
Load Config Document Method
private static XmlDocument LoadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(GetConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{ throw new Exception("No configuration file found.", e);
}
}
Get Config FilePath Method
private static string GetConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}