Config文件的读写

 1 using System;
 2 using System.Configuration;
 3 using System.Xml;
 4 
 5 namespace COMMON
 6 {
 7     public class ConfigHelperClass
 8     {
 9         public string ReadConfig()
10         {
11             return ConfigurationSettings.AppSettings["constr"].ToString();
12         }
13 
14         public void WriteConfig()
15         {
16             XmlDocument doc=new XmlDocument();    
17              //获得配置文件的全路径    
18               string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString()+ "configTest.exe.config";  
19              doc.Load(strFileName);    
20               //找出名称为“add”的所有元素    
21               XmlNodeList nodes=doc.GetElementsByTagName("add");    
22               for(int i=0;i<nodes.Count;i++)    
23               {      
24                   //获得将当前元素的key属性      
25                   XmlAttribute att=nodes[i].Attributes["key"];      
26                   //根据元素的第一个属性来判断当前的元素是不是目标元素      
27                   if (att.Value=="time")       
28                   {        
29                       //对目标元素中的第二个属性赋值       
30                       att=nodes[i].Attributes["value"];  
31                       att.Value = DateTime.Now.ToString(); ;        
32                       break;      
33                   }    
34               }    
35               //保存上面的修改    
36               doc.Save(strFileName);          
37         }
38     }
39 }
原文地址:https://www.cnblogs.com/Sunflower-/p/5531060.html