C# 配置文件Xml读写
分析xxx.exe.config文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5 </startup> 6 <system.web> 7 <membership defaultProvider="ClientAuthenticationMembershipProvider"> 8 <providers> 9 <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" /> 10 </providers> 11 </membership> 12 <roleManager defaultProvider="ClientRoleProvider" enabled="true"> 13 <providers> 14 <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" /> 15 </providers> 16 </roleManager> 17 </system.web> 18 </configuration>
在startup标签后添加:
1 <appSettings> 2 <!--版本号--> 3 <add key="version" value="0.1" /> 4 </appSettings>
读取config文件:
1 /// <summary> 2 /// 读取配置文件 3 /// </summary> 4 /// <param name="appKey">读取的键值</param> 5 public static string GetConfigValue(string appKey) 6 { 7 XmlDocument xDoc = new XmlDocument(); 8 try 9 { 10 //读取xxx.exe.config 11 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); 12 System.Xml.XmlNode xNode; 13 System.Xml.XmlElement xElem; 14 //获取节点appSettings 15 xNode = xDoc.SelectSingleNode("//appSettings"); 16 //获取对应的键值返回 17 xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']"); 18 if (xElem != null) 19 return xElem.GetAttribute("value"); 20 else 21 return ""; 22 } 23 catch 24 { 25 return ""; 26 } 27 }
写入config文件:
1 /// <summary> 2 /// 写入配置文件 3 /// </summary> 4 /// <param name="appKey">写入的键</param> 5 /// <param name="AppValue">写入的值</param> 6 public static void SetConfigValue(string AppKey, string AppValue) 7 { 8 XmlDocument xDoc = new XmlDocument(); 9 //读取xxx.exe.config 10 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); 11 12 XmlNode xNode; 13 XmlElement xElem1; 14 XmlElement xElem2; 15 //获取节点appSettings 16 xNode = xDoc.SelectSingleNode("//appSettings"); 17 //获取对应的键 18 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']"); 19 //键存在则写入新值 20 if (xElem1 != null) xElem1.SetAttribute("value", AppValue); 21 else 22 { 23 //不存在则添加新键写入新值 24 xElem2 = xDoc.CreateElement("add"); 25 xElem2.SetAttribute("key", AppKey); 26 xElem2.SetAttribute("value", AppValue); 27 xNode.AppendChild(xElem2); 28 } 29 //保存xxx.exe.config 30 xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config"); 31 }
同理可写入多条需配置的数据,如记住用户名密码:
1 <configuration> 2 <startup> 3 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 4 </startup> 5 <appSettings> 6 <!--版本号--> 7 <add key="version" value="9.9" /> 8 <!--登录参数--用户名> 9 <add key="username" value="fanhuai" /> 10 <!--登录参数--密码> 11 <add key="userpwd" value="666666" /> 12 </appSettings> 13 <system.web> 14 <membership defaultProvider="ClientAuthenticationMembershipProvider"> 15 <providers> 16 <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" /> 17 </providers> 18 </membership> 19 <roleManager defaultProvider="ClientRoleProvider" enabled="true"> 20 <providers> 21 <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" /> 22 </providers> 23 </roleManager> 24 </system.web> 25 </configuration>