C# 配置文件Xml读写
分析xxx.exe.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="" />
</providers>
</roleManager>
</system.web>
</configuration>
在startup标签后添加:
<appSettings>
<!--版本号-->
<add key="version" value="0.1" />
</appSettings>
读取config文件:
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="appKey">读取的键值</param>
public static string GetConfigValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
//读取xxx.exe.config
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
System.Xml.XmlNode xNode;
System.Xml.XmlElement xElem;
//获取节点appSettings
xNode = xDoc.SelectSingleNode("//appSettings");
//获取对应的键值返回
xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch
{
return "";
}
}
写入config文件:
/// <summary>
/// 写入配置文件
/// </summary>
/// <param name="appKey">写入的键</param>
/// <param name="AppValue">写入的值</param>
public static void SetConfigValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
//读取xxx.exe.config
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
//获取节点appSettings
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);
}
//保存xxx.exe.config
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}
同理可写入多条需配置的数据,如记住用户名密码:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<!--版本号-->
<add key="version" value="9.9" />
<!--登录参数--用户名>
<add key="username" value="fanhuai" />
<!--登录参数--密码>
<add key="userpwd" value="" />
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="" />
</providers>
</roleManager>
</system.web>
</configuration>
C# 配置文件Xml读写的更多相关文章
- 网站的配置文件XML读写
网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...
- C# XML读写实例
一.使用System.Xml 实例:完成如下格式配置文件的读写操作: <?xml version="1.0" encoding="UTF-8"?> ...
- XML读写工具
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- 配置文件Java读写
今天把配置文件的Bug修复了,总结一下Java配置文件如何读写 配置文件的格式 以.properties后缀结尾,内容不出现空格和双引号 //config.properties Driver=com. ...
- Spring MVC的配置文件(XML)的几个经典案列
1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...
- 【Python】Python XML 读写
class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer ...
- .Net 读取配置文件 xml
直接解析XML文件 1.System.Xml.Linq命名空间下提供可以使用linq查询的类,使用linq to xml读取也很方便. 2.还可以使用System.Xml.Serialization类 ...
- C语言ini格式配置文件的读写
依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...
- java文件操作(普通文件以及配置文件的读写操作)
转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...
随机推荐
- Reveal Cards In Increasing Order LT950
In a deck of cards, every card has a unique integer. You can order the deck in any order you want. ...
- 实用的JavaScript手册
实用的JavaScript手册,收藏了,需要的时候可以翻阅 包含了 什么是JavaScript: JavaScript基础知识: JavaScript内置对象 JavaScript数据类型的操作方法 ...
- 文件扩展关联命令(assoc)
assoc 命令: // 描述: (association) --> 联想.关联 显示或修改文件扩展名关联. 如果在没有参数的情况下使用,assoc将显示所有当前文件扩展名关联的列表. // 语 ...
- AndroidStudio 问题点 - app:preFUNDebugAndroidTestBuild
Error:Execution failed for task ':app:preFUNDebugAndroidTestBuild'. >Conflictwith dependency 'com ...
- mongooDb链接javaapi
mongodb链接有多种:所以不同链接下的api也不太一样. 1.api比较全面 public void query2(){ String mondburl = Config.getInstance( ...
- CPDA-战略管理
战略管理-PEST分析-市场分析-竞争环境分析-SWOT分析-内/外部因素评价矩阵-国际化/多元化战略 战略管理: 战略分析->战略制定->战略实施->战略评价->战略分析,四 ...
- SAS 操作数据集的观测
SAS 操作数据集的观测 1. SAS表达式 表达式是操作数和操作符的序列,该序列会形成一组可执行并产生 结果值的指令.其中,操作数可以是常量.变量或表达式:操作符是表 示比较.数学计算或逻辑运算的 ...
- spring深入学习(三)-----spring容器内幕
之前都是说了怎么配置bean以及用法之类的,这篇博文来介绍下spring容器内幕. 内部容器工作机制 Spring中AbstractApplicationContext抽象类的refresh()方法是 ...
- git使用之后悔药
1.工作区的代码想撤销 背景:有时候编写了一大段代码之后,想要撤销更改(执行add操作之前), 命令:git checkout -- <file路径> 使用git checkout -- ...
- Unity3D InputManager详解
首先说一下 Input 类,这个类很常用,API 大家基本都知道,这里记录几个使用频率没那么高的 API Input.acceleration:重力加速度传感器的值,加速度的方向,适用于移动平台. I ...