获取

获取应用程序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. cocos2d-x场景切换动画

    void StartScene::beginGame() {     CCLog("beginGame");          //CCTransitionScene *trans ...

  2. 学习OpenCV——BOW特征提取函数(特征点篇)

    没日没夜的改论文生活终于要告一段落了,比起改论文,学OpenCV就是一件幸福的事情.OpenCV的发展越来越完善了,已经可以直接使用BOW函数来进行对象分类了. 简单的通过特征点分类的方法:     ...

  3. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  4. 总结一下在ASP.NET中开发网站的一般步骤

    1.打开Miscosoft Visual Studio2010 2.新建网页添加新项 3.设计网页 4.添加窗体控件 5.优化页面 6.调试网页

  5. 关于Block的copy和循环引用的问题

    http://blog.csdn.net/felix9/article/details/9619313 在实际开发中,发现使用Block有着比delegate和notification更简洁的优势.于 ...

  6. Qt信号与槽应用实例一

    ..... connect(m_pGlobelWidget,signal(globeControlClick(object,object)),this,slot(globeControlClick(o ...

  7. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

  8. 欲实施CRM软件,必须先懂什么是CRM软件

    CRM是Customer Relationship Management(客户关系管理)的缩写,它是利用信息科学技术,实现市场营销.销售.服务等活动自动化,使企业能更高效地为客户提供满意.周到的服务, ...

  9. Spring中的事物管理,基于spring的bean的配置

    很多东西与上边的相同,这儿只简介: 导包... 数据库中建立三个表... 建立存放连接数据库的file文件:jdbc.properties: ----------------------------- ...

  10. 关于file的上传文件

    http://blog.csdn.net/wclxyn/article/details/7090575/