Winform讀寫App.config檔案以及重啟程式

來源:互聯網
上載者:User

標籤:

    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檔案以及重啟程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.