Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Xml;
Using System. Windows. Forms;
Namespace SZTMinorTool
{
/// <Summary>
/// App. config configuration class
/// </Summary>
Public class AppSettings
{
/// <Summary>
/// Obtain the path of the configuration file
/// </Summary>
/// <Returns> </returns>
Public static string AppConfig ()
{
Return System. IO. Path. Combine (Application. StartupPath, "App. config"); // the configuration file is in the program directory or set to the specified configuration file Path.
}
/// <Summary>
/// Get the configuration node Value
/// </Summary>
/// <Param name = "appKey"> node key value </param>
/// <Returns> </returns>
Public static string GetValue (string appKey)
{
XmlDocument xDoc = new XmlDocument ();
Try
{
XDoc. Load (AppSettings. AppConfig ());
XmlNode xNode;
XmlElement xElem;
XNode = xDoc. SelectSingleNode ("// appSettings"); // Add it to your app. config file. <appSetting> </appSetting>
XElem = (XmlElement) xNode. SelectSingleNode ("// add [@ key = '" + appKey + "']");
If (xElem! = Null)
Return xElem. GetAttribute ("value ");
Else
Return "";
}
Catch (Exception)
{
Return "";
}
}
/// <Summary>
/// Set the configuration node Value
/// </Summary>
/// <Param name = "AppKey"> key </param>
/// <Param name = "AppValue"> value </param>
Public static void SetValue (string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument ();
XDoc. Load (AppSettings. AppConfig ());
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 (AppSettings. AppConfig ());
}
}
}