Code
private void OptConfig(string AppKey, string AppValue)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
XmlDocument xmlDoc = new XmlDocument();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "YourAppName.exe.config";
xmlDoc.Load(m_strFullPath);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement) xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
private void OptConfig(string AppKey, string AppValue)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
XmlDocument xmlDoc = new XmlDocument();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "YourAppName.exe.config";
xmlDoc.Load(m_strFullPath);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement) xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}