即将操作的app.config文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--使用自定义节点必须添加如下项, 且如下项只能在Configuration中-->
<configSections>
<sectionGroup name="MySettings">
<section name="MySet" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections> <connectionStrings>
<!--无密码access数据库访问方式-->
<add name="connAcc" connectionString="Provider=Microsoft.Jet.OleDb.4.0;Data Source=db.mdb"/>
<!--有密码access数据库访问方式-->
<add name="conn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ./MyPasswordProtected.MDB;Jet OLEDB:Database Password=MyPassword;"/>
<add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ./MyPasswordProtected.MDB;Jet OLEDB:Database Password=MyPassword;"/>
</connectionStrings> <appSettings>
<!--设置替换汉字-->
<add key="splitChar" value="@"/>
<!--设置替换汉字-->
<add key="splitCharNew" value="囧"/>
<!--设置每次记忆的边框-->
<add key="biankuang" value="几字"/>
</appSettings> <MySettings>
<MySet>
<add key="8公分纸竖1" value="2.7,23,-10,0.76,230"/>
<add key="8公分纸竖2" value="2,23,30,0.81,230"/>
<add key ="8公分纸横" value ="1.5,23,-10,0.76,220"/>
<add key ="彩带" value="10,23,30,0.79,220"/>
<add key ="11公分竖" value="4,23,45,0.79,340"/>
<add key ="11公分横" value ="4,34,30,0.76,340"/>
</MySet>
</MySettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

OperatAppConfig类内容如下, 我觉得使用微软的ConfigurationManager 在没有自定义节点时非常方便, 但是如果涉及到自定义节点后将会比较麻烦, 所以莫不如直接把app.config当做xml文件, 直接操作(需要注意的是程序在运行时其实执行的是appName.exe.config文件, 并非app.config文件):

 /// <summary>
/// 对程序运行中的appConfig进行读写
/// </summary>
public class OperatAppConfig
{
public static bool DelXmlNode(string appNode, string key)
{
try
{
string strPath = System.Windows.Forms.Application.ExecutablePath + ".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strPath); XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(appNode); foreach (XmlNode item in xNode.ChildNodes )
{
// System.Console.WriteLine(item.Attributes[0].Value);
if (item.Attributes[0].Value == key)
{
xNode.RemoveChild(item);
}
} xDoc.Save(strPath);
return true;
}
catch (System.Exception ex)
{
return false;
}
} /// <summary>
/// 对指定的节点添加子节点
/// </summary>
/// <param name="appNode">指定的节点名称一般为//MySettings//MySet格式</param>
/// <param name="key">这个方法只添加add, 所以这里需要给出key和value</param>
/// <param name="value">这个方法只添加add, 所以这里需要给出key和value</param>
/// <returns></returns>
public static bool AddXmlNode(string appNode,string key,string value)
{
try
{
string strPath = System.Windows.Forms.Application.ExecutablePath + ".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strPath); XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(appNode); XmlElement newElement = xDoc.CreateElement("add");
// newElement.InnerText = "black";
newElement.SetAttribute("key", key);//添加一个带有属性的节点信息
newElement.SetAttribute("value", value);
xNode.AppendChild(newElement); //追加到xNode下
//保存更改
xDoc.Save(strPath);
return true;
}
catch (System.Exception ex)
{
return false;
}
} /// <summary>
/// 获取某个指定节点下的所有key
/// </summary>
/// <param name="appKey"></param>
/// <param name="appValue"></param>
public static System.Collections.Generic.List<string> GetKeys(string appNode)
{
System.Collections.Generic.List<string> xmlNodes = new System.Collections.Generic.List<string>();
try
{ XmlDocument xDoc = new XmlDocument();
//要注意的是使用ExecutablePath所获取的一般是 appName.exe.config文件中的内容, 而不是appName.config中的内容
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode = xDoc.SelectSingleNode(appNode); foreach (XmlElement item in xNode)
{
xmlNodes.Add(item.Attributes[0].Value + "囧" + item.Attributes[1].Value);
}
return xmlNodes;
}
catch
{
xmlNodes.Clear();
xmlNodes.Add("默认值囧2,23,0,0.9,220");
return xmlNodes;
}
} /// <summary>
/// 根据键设置值
/// </summary>
/// <param name="appKey"></param>
/// <param name="appValue"></param>
public static void SetAppConfig(string appKey, string appValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); var xNode = xDoc.SelectSingleNode("//appSettings"); var xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
xElem.SetAttribute("value", appValue);
else
{
var xNewElem = xDoc.CreateElement("add");
xNewElem.SetAttribute("key", appKey);
xNewElem.SetAttribute("value", appValue);
xNode.AppendChild(xNewElem);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
} /// <summary>
/// 根据键查找值
/// </summary>
/// <param name="appKey"></param>
/// <returns></returns>
public static string GetAppConfig(string appKey)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); var xNode = xDoc.SelectSingleNode("//appSettings"); var xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']"); if (xElem != null)
{
return xElem.Attributes["value"].Value;
}
return string.Empty;
} /// <summary>
/// 根据键查找值
/// </summary>
/// <param name="appKey"></param>
/// <returns></returns>
public static char GetAppConfigChar(string appKey)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); var xNode = xDoc.SelectSingleNode("//appSettings"); var xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']"); if (xElem != null)
{
return XmlConvert.ToChar(xElem.Attributes["value"].Value);
}
}
catch { return '囧';
}
return '囧';
}
}

调用:

//获取此节点下所有的key
OperatAppConfig.GetKeys("//MySettings//MySet");
//删除此节点下指定key的节点
bool isDel = Common.OperatAppConfig.DelXmlNode("//MySettings//MySet", this.txtZKName.Text.Trim());
//在指定节点下新增节点
bool isAdd = Common.OperatAppConfig.AddXmlNode("//MySettings//MySet", this.txtZKName.Text.Trim(), strValue);
//根据键找值
OperatAppConfig.GetAppConfigChar("splitCharNew")

C#简单操作app.config文件的更多相关文章

  1. 第19课-数据库开发及ado.net ADO.NET--SQLDataReader使用.SqlProFiler演示.ADoNET连接池,参数化查询.SQLHelper .通过App.Config文件获得连接字符串

    第19课-数据库开发及ado.net ADO.NET--SQLDataReader使用.SqlProFiler演示.ADoNET连接池,参数化查询.SQLHelper .通过App.Config文件获 ...

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

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

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

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

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

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

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

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

  6. 操作App.config的类(转载)

    http://www.cnblogs.com/yaojiji/archive/2007/12/17/1003191.html 操作App.config的类 public class DoConfig  ...

  7. 配置文件——App.config文件读取和修改

    作为普通的xml文件读取的话,首先就要知道怎么寻找文件的路径.我们知道一般配置文件就在跟可执行exe文件在同一目录下,且仅仅在名称后面添加了一个.config 因此,可以用Application.Ex ...

  8. WPF C#之读取并修改App.config文件

    原文:WPF C#之读取并修改App.config文件 简单介绍App.config App.config文件一般是存放数据库连接字符串的.  下面来简单介绍一下App.config文件的修改和更新. ...

  9. Visual Studio 2013 Unit Test Project App.config文件设置方法

    开放中经常会要做单元测试,新的项目又没有单元测试项目,怎么才能搭建一个单元测试项目呢? 下面跟我四步走,如有错误之处,还请指正! 1.添加项目 2.添加配置文件 新建app.config文件,注意不是 ...

随机推荐

  1. 项目中如何使用EF

    本文将在技术层面挑战园子里的权威大牛们,言语不敬之处敬请包涵.本文旨为技术交流,欢迎拍砖. 园子里面分享和推荐Entity Framework(以下简称EF)的Repository(仓储)设计模式的文 ...

  2. 方便处理hosts的批处理脚本:hosts助手.bat

    hosts助手.bat @echo off pushd "%~dp0" set sp0=------------------ set sp1=hosts助手 set hostsfi ...

  3. ionic2——开发利器之Visual Studio Code 常用快捷键

    主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace 会进入到 Ctrl+P 模式 在 Ctrl+P 下输入 >  ...

  4. maven pox.xml文件报错解决办法 亲测可以

    问题1 由于maven下载依赖包失败导致一些文件没完全下载下来,形成了lastUpdated结尾的文件存放在本地仓库中(我是默认地址:C:\Users\Administrator\.m2\reposi ...

  5. Mongodb 补充

    1 mongodb 概述 启动mongo 1 数据库操作 没有数据的 集合 和 数据库不会显示 db 查看当前的数据库名称: 所有物理上存在的数据库 db.stats() 查看当前的数据库信息: sh ...

  6. 安装nodejs+npm的体验

    NODEJS.NPM安装配置步骤(WINDOWS版本) 1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://no ...

  7. C++纯虚函数实现

    纯虚函数就是一个在基类中的虚函数,差别只是在一般的虚函数声明的后面加了"=0",虚函数允许函数通过与函数体之间的联系在运行时才建立,也就是在运行时才决定如何动作,称为运行时的多态性 ...

  8. ngxtop实时监控nginx状态

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  9. bzoj 4448 情报传递

    Written with StackEdit. Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有\(n\)名情报员.每名情报员能有若干名(可能没有)下线,除\ ...

  10. UVA11174 Stand in a Line

    题意 PDF 分析 \[ f(i)=f(c_1)f(c_2)\dots\times(s(i)-1)!/(s(c_1)!s(c_2)! \dots s(c_k)! )\\ f(root)=(s(root ...