获取

获取应用程序exe.config文件中  节点value值

/// <summary>
/// 功能: 读取应用程序exe.config文件中
/// appSettings节点下 节点add属性值
/// 根据add的属性值key来读取value值
/// </summary>
/// <param name="appKey">属性key值</param>
/// <returns></returns>
public string GetConfigValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
//System.Windows.Forms.Application.ExecutablePath可执行文件路径(包括执行文件名称)
//加载文件
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem;
//选取appSettings节点
xNode = xDoc.SelectSingleNode("//appSettings");
//根据key属性值appKey选择节点
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
//返回value属性值
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}

 

上面是方法:
下面为调用:
GetConfigValue("key值");

修改

上面是方法:

修改应用程序exe.config文件及App.config中 节点value值

/// <summary>
/// 功能:动态配置app.config
/// </summary>
/// <param name="AppKey">节点属性key值</param>
/// <param name="AppValue">节点属性value值</param>
private void SetValue(string AppKey, string AppValue)
{
for (int i = 0; i < 2; i++)
{
XmlDocument doc = new XmlDocument();
doc.Load(AppConfig(i));
XmlNode node = doc.SelectSingleNode(@"//appSettings");
XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@key='" + AppKey + "']");
ele.SetAttribute("value", AppValue);
doc.Save(AppConfig(i));
}
}
/// <summary>
/// 功能:重置数据库connectionStrings连接字符串
/// 时间:2013年12月31日11:09:57
/// </summary>
/// <param name="strKey">节点属性name值</param>
/// <param name="strValue">节点属性connectionString值</param>
/// <returns></returns>
private void WriteXml(string strKey, string strValue)
{
//循环2次
//分别修改App.config及应用程序exe.config connectionStrings节点值
for (int i = 0; i < 2; i++)
{
XmlDocument doc = new XmlDocument();
doc.Load(AppConfig(i));
XmlNode node = doc.SelectSingleNode(@"//connectionStrings");
XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@name='" + strKey + "']");
ele.SetAttribute("connectionString", strValue);
doc.Save(AppConfig(i));//保存
}
}

/// <summary>
/// 功能:获取配置文件路径
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public string AppConfig(int i)
{
if (i == 0)//获取应用程序目录下App.config路径
{
int intPos = Application.StartupPath.Trim().IndexOf("bin") - 1;
string strDirectoryPath = System.IO.Path.Combine(Application.StartupPath.Substring(0, intPos), "App.config");
return strDirectoryPath;
}
else//获取应用程序exe目录 如:MultiThreadDemo.exe.config目录路径
{
return System.Windows.Forms.Application.ExecutablePath + ".config";
}
}

 

下面为调用:

winform app.config文件的动态配置的更多相关文章

  1. 关于 App.config文件出错,配置系统未能初始化。 问题解决方案

    如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素.将appSettings放到configSecti ...

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

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

  3. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  4. winform客户端程序实时读写app.config文件

    新接到需求,wcf客户端程序运行时,能实时修改程序的打印机名称: 使用XmlHelper读写 winform.exe.config文件修改后始终,不能实时读取出来,查询博客园,原来已有大神解释了: 获 ...

  5. C#中App.config文件配置获取

    最新的framework使用如下方法: using System.Configuration; ConfigurationManager.AppSettings["key"]; A ...

  6. C#项目实例中读取并修改App.config文件

    C#项目是指一系列独特的.复杂的并相互关联的活动,这些活动有着一个明确的目标或目的,必须在特定的时间.预算.资源限定内,依据规范完成.项目参数包括项目范围.质量.成本.时间.资源. 1. 向C#项目实 ...

  7. C# App.config文件的使用

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

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

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

  9. C#项目中关于多个程序集下App.config文件的问题

    在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice.wcf服务时生成:也有手动建立的配置文件直接默认名就为app.config.这些配置有的保存当前程序集用 ...

随机推荐

  1. ios - 文件保存路径的获取

    "应用程序包": 这里面存放的是应用程序的源文件,包括资源文件和可执行文件. 获取方式: NSString *path = [[NSBundle mainBundle] bundl ...

  2. Web3D编程入门总结——面向对象的基础Web3D框架

    本篇主要通过分析Tony Parisi的sim.js库(原版代码托管于:https://github.com/tparisi/WebGLBook/tree/master/sim),总结基础Web3D框 ...

  3. Unity基于响应式编程(Reactive programming)入门

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  4. Leetcode: Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  5. hihocoder 网易游戏2016实习生招聘在线笔试 解题报告

    比赛笔试链接:http://hihocoder.com/contest/ntest2015april/problems 题目就不贴了. 1.推箱子. 思路:纯模拟. 代码(28MS): #includ ...

  6. C#使用二叉树算法设计一个无限分级的树表

    效果图: 数据库: 操作树的示意图: 控制器代码: using Dw.Business; using Dw.Entity; using Dw.Utilities; using System; usin ...

  7. 如何拥有一个自己的Vagrant box

    这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...

  8. Qt报表控件NCReport教程:报表创建示例

    NCReport是 一款10多年时间的老牌报表控件,最初是在2002年时作为qt3的应用程序的一个联合项目,后来就成为了一个独立的GPL项目.现在的NCReport 是一款轻量级.快速.多平台.简单易 ...

  9. tomcat部署https

    在server.xml配置文件中增加证书位置跟密码: <Connector port="443" protocol="org.apache.coyote.http1 ...

  10. [windows API]获取当前系统图标,文字大小

     取DPI 缩放比例 HWND wnd = ::GetDesktopWindow(); dbg_print("desktopwnd:0x%X\n",wnd); HDC dc = G ...