- Restarting the main program
- System.Diagnostics.Process.Start (System.Reflection.Assembly.GetExecutingAssembly (). Location);
- #region Read the App. config field value
- Public static string Getconfigvalue (string appKey)
- {
- XmlDocument XDoc = new XmlDocument ();
- Try
- {
- //cache path
- Xdoc.load (System.Windows.Forms.Application.ExecutablePath + ". config");
- System.Xml.XmlNode XNode;
- System.Xml.XmlElement Xelem;
- XNode = Xdoc.selectsinglenode ("//appsettings");
- Xelem = (System.Xml.XmlElement) xnode.selectsinglenode ("//add[@key = '" + AppKey + "']");
- if (xelem! = null)
- return Xelem.getattribute ("value");
- Else
- return "";
- }
- Catch
- {
- return "";
- }
- }
- Public static void Setconfigvalue (string AppKey, string appvalue)
- {
- XmlDocument XDoc = new XmlDocument ();
- Xdoc.load (System.Windows.Forms.Application.ExecutablePath + ". config");
- 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 (System.Windows.Forms.Application.ExecutablePath + ". config");
- }
- #endregion
//Restarting the main program//System.Diagnostics.Process.Start (System.Reflection.Assembly.GetExecutingAssembly (). Location); #regionRead and save the App. config field value Public Static stringGetconfigvalue (stringAppKey) {XmlDocument XDoc=NewXmlDocument (); Try { //Cache PathXdoc.load (System.Windows.Forms.Application.ExecutablePath +". config"); System.Xml.XmlNode XNode; System.Xml.XmlElement Xelem; XNode= Xdoc.selectsinglenode ("//appsettings"); Xelem= (System.Xml.XmlElement) Xnode.selectsinglenode ("//add[@key = '"+ AppKey +"']"); if(Xelem! =NULL) returnXelem.getattribute ("value"); Else return ""; } Catch { return ""; } } Public Static voidSetconfigvalue (stringAppKey,stringappvalue) {XmlDocument XDoc=NewXmlDocument (); Xdoc.load (System.Windows.Forms.Application.ExecutablePath+". config"); 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 (System.Windows.Forms.Application.ExecutablePath+". config"); } #endregion
WinForm Read and Write app. config file and restart program