码迷,mamicode.com
首页 > 移动开发 > 详细

Winform读写App.config文件以及重启程序

时间:2016-08-07 06:23:45      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

    1. //重启主程序  
    2. //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);  
    3. #region 读存app.config字段值  
    4. public static string GetConfigValue(string appKey)  
    5. {  
    6.     XmlDocument xDoc = new XmlDocument();  
    7.     try  
    8.     {  
    9.         //缓存路径  
    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.   
    26.   
    27. public static void SetConfigValue(string AppKey, string AppValue)  
    28. {  
    29.     XmlDocument xDoc = new XmlDocument();  
    30.     xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");  
    31.   
    32.     XmlNode xNode;  
    33.     XmlElement xElem1;  
    34.     XmlElement xElem2;  
    35.     xNode = xDoc.SelectSingleNode("//appSettings");  
    36.   
    37.     xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=‘" + AppKey + "‘]");  
    38.     if (xElem1 != null) xElem1.SetAttribute("value", AppValue);  
    39.     else  
    40.     {  
    41.         xElem2 = xDoc.CreateElement("add");  
    42.         xElem2.SetAttribute("key", AppKey);  
    43.         xElem2.SetAttribute("value", AppValue);  
    44.         xNode.AppendChild(xElem2);  
    45.     }  
    46.     xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");  
    47. }  
    48. #endregion 

 

        //重启主程序
        //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
        #region 读存app.config字段值
        public static string GetConfigValue(string appKey)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                //缓存路径
                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

 

Winform读写App.config文件以及重启程序

标签:

原文地址:http://www.cnblogs.com/webenh/p/5745354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!