WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   WebEnh  阅读(476)  评论(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
复制代码

 

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多