分析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读写的更多相关文章

  1. 网站的配置文件XML读写

    网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...

  2. C# XML读写实例

    一.使用System.Xml 实例:完成如下格式配置文件的读写操作: <?xml version="1.0" encoding="UTF-8"?> ...

  3. XML读写工具

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  4. 配置文件Java读写

    今天把配置文件的Bug修复了,总结一下Java配置文件如何读写 配置文件的格式 以.properties后缀结尾,内容不出现空格和双引号 //config.properties Driver=com. ...

  5. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  6. 【Python】Python XML 读写

    class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer ...

  7. .Net 读取配置文件 xml

    直接解析XML文件 1.System.Xml.Linq命名空间下提供可以使用linq查询的类,使用linq to xml读取也很方便. 2.还可以使用System.Xml.Serialization类 ...

  8. C语言ini格式配置文件的读写

    依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...

  9. java文件操作(普通文件以及配置文件的读写操作)

    转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...

随机推荐

  1. rtx tiny os

    一,简单测试步骤: 1.  license management包含两项:artx51 real time os, PK51 2. code #include "rtx51tny.h&quo ...

  2. 解决win系统无法安装.NET Framework 4.0 4.6 原因是HRESULT0xc8000222

    1.开始----- 运行------- cmd ----- 键入net stop WuAuServ回车(停止windows update服务) 2.开始----- 运行----键入%windir%回车 ...

  3. 2019.03.25 bzoj4568: [Scoi2016]幸运数字(倍增+线性基)

    传送门 题意:给你一棵带点权的树,多次询问路径的最大异或和. 思路: 线性基上树?? 倍增维护一下就完了. 时间复杂度O(nlog3n)O(nlog^3n)O(nlog3n) 代码: #include ...

  4. pychrom 中文版

    http://jingyan.baidu.com/article/a378c960daf80eb328283033.html

  5. selenium3+python3.6爬页面源码的代码

    from selenium import webdriver import unittest,time class my_test(unittest.TestCase): def setUp(self ...

  6. 通过HttpClient发起Get请求,获取Json数据,然后转为java数据,然后批量保存数据库;

    Json转java所需Jar包: commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,comm ...

  7. NotePad++ 添加HEX-Editor插件

    步骤: 一.下载插件 https://github.com/chcg/NPP_HexEdit/releases 二.选择插件版本 Notepad 官网客服提示:32bit Notepad++可以使用常 ...

  8. X86给龙芯笔记本编译本地工具链(未完待续)

    我买了一台龙芯2F的笔记本来当玩具. 买回来发现,这台笔记本上没法安装软件,因为既没有软件仓库,也没有GCC. 因此需要构建交叉工具链和构建本地工具链. 下面是我研究如何搞定着一切的笔记. 工具链组件 ...

  9. mac下crontab定时任务使用

    这篇文章的作用 BREAK TIME 本地pc配置定时任务,开机后每隔一小时执行一次,open这个页面,休息半分钟 cron创建备忘 首先创建定时任务 crontab -e 0 */ * * * op ...

  10. SSRF

    SSRF 关于SSRF SSRF(Server-Side Request Forgery:服务器端请求伪造),攻击者通过伪造服务器端发起的请求,获取客户端所不能得到的数据.一般情况下,SSRF攻击的目 ...