WinForm Read and Write app. config file and restart program

Source: Internet
Author: User

    1. Restarting the main program
    2. System.Diagnostics.Process.Start (System.Reflection.Assembly.GetExecutingAssembly (). Location);
    3. #region Read the App. config field value
    4. Public static string Getconfigvalue (string appKey)
    5. {
    6. XmlDocument XDoc = new XmlDocument ();
    7. Try
    8. {
    9. //cache path
    10. Xdoc.load (System.Windows.Forms.Application.ExecutablePath + ". config");
    11. System.Xml.XmlNode XNode;
    12. System.Xml.XmlElement Xelem;
    13. XNode = Xdoc.selectsinglenode ("//appsettings");
    14. Xelem = (System.Xml.XmlElement) xnode.selectsinglenode ("//add[@key = '" + AppKey + "']");
    15. if (xelem! = null)
    16. return Xelem.getattribute ("value");
    17. Else
    18. return "";
    19. }
    20. Catch
    21. {
    22. return "";
    23. }
    24. }
    25. Public static void Setconfigvalue (string AppKey, string appvalue)
    26. {
    27. XmlDocument XDoc = new XmlDocument ();
    28. Xdoc.load (System.Windows.Forms.Application.ExecutablePath + ". config");
    29. XmlNode XNode;
    30. XmlElement xElem1;
    31. XmlElement xElem2;
    32. XNode = Xdoc.selectsinglenode ("//appsettings");
    33. XElem1 = (XmlElement) xnode.selectsinglenode ("//add[@key = '" + AppKey + "']");
    34. if (xElem1! = null) xelem1.setattribute ("value", appvalue);
    35. Else
    36. {
    37. XELEM2 = Xdoc.createelement ("add");
    38. Xelem2.setattribute ("key", AppKey);
    39. Xelem2.setattribute ("value", appvalue);
    40. Xnode.appendchild (XELEM2);
    41. }
    42. Xdoc.save (System.Windows.Forms.Application.ExecutablePath + ". config");
    43. }
    44. #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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.