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. Servlet生命周期中的service方法分析

    问题ServletLifeCycle中的service方法内,有super.service(request, response); 会执行this.doGet(HttpServletRequest r ...

  2. SQLProfiler_SQL抓包

    有时候我们的某个程序或者应用在执行SQL语句时报错了, 我们需要拿到报错的SQL语句检查, 那么你可以借助:SQL Profiler工具来实现. 1.SQL Profiler是一个可以检测SQL服务器 ...

  3. 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native

    [源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...

  4. SSH实例(3)

    src文件夹的结构如下: clasDao文件: package com.school.service; import java.util.List; import com.school.dao.Cla ...

  5. 后缀名为properties,config和xml的文件内容读取

    1.先建立文件(后缀名为properties和config) 2.读取类建立 public class Read{ public static Properties  properties = new ...

  6. jquery学习笔记:获取下拉框的值和下拉框的txt

    <div class="form-group"> <select class="form-control" id="iv_level ...

  7. Foreach原理

    本质:实现了一个IEnumerable接口, 01.为什么数组和集合可以使用foreach遍历? 解析:因为数组和集合都实现了IEnumerable接口,该接口中只有一个方法,GetEnumerato ...

  8. jQuery8种不同的瀑布流懒加载loading效果

    优化图片加载插件jQuery8种不同的瀑布流懒加载loading效果  在线预览 下载地址 实例代码 <ul class="grid effect-1" id="g ...

  9. C#实现类似"hello $world"的格式化字符串方法

    C#自带的string.Format可以格式化字符串,但是还是不太好用,由于格式的字符占位符都是数字,当数目较多时容易混淆.其实可以扩展string的方法,让C#的字符串具备其他的方法,下面介绍一个实 ...

  10. Summernote – 基于 Bootstrap 的文本编辑器

    Summernote 是一个简单,灵活,所见即所得(WYSIWYG)的编辑器,基于 jQuery 和 Bootstrap 构建.Summernote 所有主要的操作都支持快捷键,有一个功能强大的 AP ...