Winform读写App.config文件以及重启程序
- //重启主程序
- //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
//重启主程序
//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文件以及重启程序的更多相关文章
- WinForm读写App.config配置文件
一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...
- 【C#】【WPF】如何读写app.config文件
WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...
- Winform 数据库连接app.config文件配置 数据库连接字符串
1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...
- Winform数据库连接app.config文件配置
1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...
- 关于读写APP.config文件能读却写不了的问题
今天要求用winform写一个窗口用来读写一个App.config,要对 <appSettings>里面的add key和value进行添加和修改.要实现的效果图如下: -------- ...
- C#简单操作app.config文件
即将操作的app.config文件内容如下 <?xml version="1.0" encoding="utf-8"?> <configura ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
- WPF程序中App.Config文件的读与写
WPF程序中的App.Config文件是我们应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,所以它是一种高效的程序配置方式,那么今天我就 ...
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
随机推荐
- .NET中类(class)与结构(struct)
结构是值类型:值类型在栈上分配空间: 类是引用类型:引用类型在堆栈上分配空间: 虽然结构与类的类型不一样,可是他们的基类型都是对象(object),c#中所有类型的基类型都是object: 虽然 ...
- HTTP必知必会
HTTP协议作为网络传输的基本协议,有着广泛的应用.HTTP协议的完整内容很多,但是其核心知识却又简单精炼.学习者应该掌握其基本结构,并且能够举一反三.这篇文章所列的,就是在实际开发中必须知道必须掌握 ...
- ASP.NET MVC ModelValidator小结
当用户通过UI输入数据向程序交互时,都会出现一个潜在的错误,数据错误,要检查用户提交的数据是否正确,需要做数据验证,在ASP.NET MVC中,每当Action执行前都会对传入Action的Model ...
- 有关微信开发中errorcode:-1 errmsg:system error 错误的一点原因
如果你在微信开发中遇到{"errcode":-1,"errmsg":"system error,hints:[req_id:]"}这样的错误 ...
- Widget的点击事件
本文实现Widget中的按钮点击事件,点击一次下面的按钮,上面的数字减少1. 首先是Manifest文件: <?xml version="1.0" encoding=&quo ...
- 【Java每日一题】20161116
package Nov2016; public class Ques1116 { public static void main(String[] args){ System.out.println( ...
- LoadRunner上传及下载文件
(1)LoadRunner上传文件 web_submit_data("importStudent.do", "Action=https://testserver/cons ...
- 国际化支持(I18N)
本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是 ...
- jQuery als.js 跑马灯
ali.js是一款滚动插件,滚动的内容可包含文字和图片.它的API也很强大,包括滚动区域可见个数.每次滚动个数.滚动方向.是否循环滚动.是否自动滚动.滚动间隔时间.滚动动画速度.动画效果.滚动方向以及 ...
- javascript数据类型理解整理
起因:关于数据类型这块,自己看了很多遍相关的资料,每次查看和实践都有一些体会和理解:但又感到没有理解透,总是差一点,最近又在看这块的内容,加上最近的积累,做个相关笔记 ECMAScript数据类型:1 ...