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


      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
  • 相关阅读:
    Android 编译笔记20191205
    react 编写 基于ant.design 页面的参考笔记
    Codeigniter项目使用phpDocumentor生成api文档
    php curl Problem with the SSL CA cert (path access rights)
    我的浏览器标签同步方案:坚果云+Floccus
    vue Inline JavaScript is not enabled. Is it set in your options?
    学习应该保留的十件事情
    ngx-moment汉化
    Quill Editor使用公式
    mac os安装多个版本的chrome
  • 原文地址:https://www.cnblogs.com/webenh/p/5745354.html
Copyright © 2020-2023  润新知