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. 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
        //重启主程序
//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文件以及重启程序的更多相关文章

  1. WinForm读写App.config配置文件

    一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  2. 【C#】【WPF】如何读写app.config文件

    WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...

  3. Winform 数据库连接app.config文件配置 数据库连接字符串

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  4. Winform数据库连接app.config文件配置

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  5. 关于读写APP.config文件能读却写不了的问题

    今天要求用winform写一个窗口用来读写一个App.config,要对  <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...

  6. C#简单操作app.config文件

    即将操作的app.config文件内容如下 <?xml version="1.0" encoding="utf-8"?> <configura ...

  7. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  8. WPF程序中App.Config文件的读与写

    WPF程序中的App.Config文件是我们应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,所以它是一种高效的程序配置方式,那么今天我就 ...

  9. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

随机推荐

  1. web前端学习笔记---实现雪花飘落的效果

    看了javascript网页特效实例大全中的图片飘下的效果实例,觉得值得动手学习下. 就把图片改成雪花图,完成一个雪花飘下的效果. 并且,其中有些内容比较陈旧了,那么就学者改掉吧. 包括: 1.对le ...

  2. 质数的判断,实现bool IsPrime(int number)

    1.重复输入一个数,判断该数是否是质数,输入q结束?质数的判断用方法来实现bool IsPrime(int number) static void Main(string[] args) { // 要 ...

  3. C++移位运算符

    关于逻辑移位.算术移位可参见迅雷深大笔试题部分.的一道题. 以前看到C++标准上说,移位运算符(<<.>>)出界时的行为并不确定: The behavior is undefi ...

  4. java的各种类型转换汇总

    java类型转换 Integer String Long Float Double Date 1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Intege ...

  5. WPFINotifyPropertyChanged使用技巧

    WPFINotifyPropertyChanged使用技巧 public class NotifyPropertyChanged : INotifyPropertyChanged { public e ...

  6. ASP.NET MVC 模块与组件(二)——定制图片验证码

     本着简洁直接,我们就直奔主题吧! 下面是一个生成数字和字母随机组合的验证码类源代码: using System; using System.Drawing; using System.Drawing ...

  7. 如何优雅地使用 Windows 10 ?

    身为一个伪电脑Geek,就算不懂电脑,也要把那些更不懂电脑的人给唬住. 1.入门级别: 当然就是让别人从视觉上感觉你的电脑不一般啦 第一步:桌面要干净,干净到什么程度呢,要像这样 当别人第一眼看到你的 ...

  8. hibernate 注释说明

    * @Entity -- 将一个类声明为一个实体 bean(即一个持久化 POJO 类) * @Id -- 注解声明了该实体 bean 的标识属性(对应表中的主 键). * @Table -- 注解声 ...

  9. JMS中的消息通信模型

    1. MQ简介: 消息队列(Message Queue,简称MQ),是应用程序与应用程序之间的一种通信方法.应用程序通过发送和检索出入列队的针对应用程序的数据 - 消息来通信,而无需专用连接来链接它们 ...

  10. 算法:冒泡排序(python版)

    1.将n个元素从大到小排序后,选择第k个大的元素 #!/usr/bin/env python #coding -*- utf:8 -*- #冒泡排序选第k个元素 import random impor ...