C# 配置文件Xml读写


分析xxx.exe.config文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3   
 4     "v4.0" sku=".NETFramework,Version=v4.5" />
 5   
 6   
 7     "ClientAuthenticationMembershipProvider">
 8       
 9         "ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
10       
11     
12     "ClientRoleProvider" enabled="true">
13       
14         "ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
15       
16     
17   
18 

在startup标签后添加:

1   
2     
3     "version" value="0.1" />
4   

读取config文件:

 1         /// 
 2         /// 读取配置文件
 3         /// 
 4         /// 读取的键值 
 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 /// 
 2 /// 写入配置文件
 3 /// 
 4 /// 写入的键 
 5 /// 写入的值 
 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 
 2   
 3     "v4.0" sku=".NETFramework,Version=v4.5" />
 4   
 5   
 6     
 7     "version" value="9.9" />
 8     
							
C