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文 ...
随机推荐
- English Conversations You Can Download for Free (Spoken English MP3/Audio Files)
If you want to download free English conversations, you’ve come to the right place. This page introd ...
- vue.js报错:Module build failed: Error: No parser and no file path given, couldn't infer a parser.
ERROR Failed to compile with 2 errors 12:00:33 error in ./src/App.vue Module build failed: Error: No ...
- Centos7 登录mysql 出现mysql.sock不存在
记一次纠错大战 本来今天开开心心地部署nginx和uwsgi到我的Django服务器,刚想给老师演示一遍我这项目如何酷炫时,一登陆就出现mysql连接不上了 (2003, "Can't co ...
- CentOS 7 rpm安装jdk
RPM 安装jdk1.8.0_111 ,查询系统自带的jdk rpm -qa | grep java 查询结果如下: [root@bogon ~]# rpm -qa | grep java javap ...
- expdp错误案例
转自:https://www.cnblogs.com/kerrycode/p/3960328.html Oracle数据泵(Data Dump)使用过程当中经常会遇到一些奇奇怪怪的错误案例,下面总结一 ...
- KITTI数据集的使用——雷达与相机的数据融合
目录 目的 如何实现 kitti数据集简介 kitti数据集的raw_data 利用kitti提供的devkit以及相应数据集的calib文件 解读calib文件夹 解读devkit 目的 使用雷达点 ...
- jsp获取当前项目跟路径
在jsp中获取当前项目的根路径: <% String basePath = request.getScheme() + "://"+ request.getServerNam ...
- java(三)数据库部分
3.1.1.数据库的分类及常用的数据库 数据库分为:关系型数据库和非关系型数据库 关系型:mysql oracle sqlserver等 非关系型:redis,memcache,mogodb,hado ...
- 学习Python第五天
今天咱们讲一下元组,hash,字典,元组是数据类型其中之一 元组的特性为有序的,不可变的,但是如果其中有可变元素,这些可变元组还是可以改变的,代码如下: 元组的写法:name = (‘roy’,‘al ...
- 10.1牛客J题
https://www.nowcoder.com/acm/contest/201/J Description: 给你一行括号,定义了括号合格的形式,然后Q次询问,问你这个区间内括号是否合格 Solut ...