配置文件的根节点是configuration。我们经常访问的是appSettings,它是由.Net预定义配置节。我们经常使用的配置文件的架构是象下面的形式。
先大概有个印象,通过后面的实例会有一个比较清楚的认识。下面的“配置节”可以理解为进行配置一个XML的节点。
1. 向项目添加 app.config 文件:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml; 6 using System.Configuration; 7 8 namespace Demo 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 string serverIP = ConfigurationManager.AppSettings["ServerIP"]; 15 string dataBase = ConfigurationManager.AppSettings["DataBase"]; 16 string user = ConfigurationManager.AppSettings["user"]; 17 string password = ConfigurationManager.AppSettings["password"]; 18 Console.WriteLine(serverIP); 19 Console.WriteLine(dataBase); 20 Console.WriteLine(user); 21 Console.WriteLine(password); 22 Console.ReadKey(); 23 } 24 } 25 }
但是要想调用ConfigurationManager必须要先在工程里添加system.configuration程序集的引用。
(在解决方案管理器中右键点击工程名称,在右键菜单中选择添加引用,.net 标签页下即可找到)
1 // 所有的Key遍历出后保存在一个容器里(例如:数组),然后用Key匹配找出Value即可 2 static void Main(string[] args) 3 { 4 //判断App.config配置文件中是否有Key(非null) 5 if (ConfigurationManager.AppSettings.HasKeys()) 6 { 7 List<string> theKeys = new List<string>(); //保存Key的集合 8 List<string> theValues = new List<string>(); //保存Value的集合 9 //遍历出所有的Key并添加进theKeys集合 10 foreach (string theKey in ConfigurationManager.AppSettings.Keys) 11 { 12 theKeys.Add(theKey); 13 } 14 //根据Key遍历出所有的Value并添加进theValues集合 15 for (int i = 0; i < theKeys.Count; i++) 16 { 17 foreach (string theValue in ConfigurationManager.AppSettings.GetValues(theKeys[i])) 18 { 19 theValues.Add(theValue); 20 } 21 } 22 //验证一下 23 Console.WriteLine("*************Key*************"); 24 foreach (string s in theKeys) 25 { 26 Console.WriteLine(s); 27 } 28 Console.WriteLine("************Value************"); 29 foreach (var item in theValues) 30 { 31 Console.WriteLine(item); 32 } 33 } 34 Console.ReadKey(); 35 }
结果如下:
1 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 2 config.AppSettings.Settings["user"].Value = "demo"; 3 config.Save(ConfigurationSaveMode.Full);
注意,正确的做法是,把配置文件按照普通的xml文件来进行修改,否则,经常出现的问题就是你所修改的东西其实最后根本没有写入文件!最后还是修改失败!
当作普通的xml文件读取的话,首先就要知道怎么寻找文件的路径。我们知道一般配置文件就在跟可执行exe文件在同一目录下,且仅仅在名称后面添加了一个.config 因此,可以用Application.ExecuteablePath+".cofig"的方式来获得,不过更加推荐使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile这句话来直接获取当前程序的配置文件的位置,具体原因,后面再叙述。
这里给出我常用的操作函数
1 using System.Xml; 2 //第一个参数是xml文件中的add节点的value,第二个参数是add节点的key 3 private void SaveConfig(string ConnenctionString, string strKey) 4 { 5 XmlDocument doc = new XmlDocument(); 6 //获得配置文件的全路径 7 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 8 // string strFileName= AppDomain.CurrentDomain.BaseDirectory + "\\exe.config"; 9 doc.Load(strFileName); 10 //找出名称为“add”的所有元素 11 XmlNodeList nodes = doc.GetElementsByTagName("add"); 12 for (int i = 0; i < nodes.Count; i++) 13 { 14 //获得将当前元素的key属性 15 XmlAttribute att = nodes[i].Attributes["key"]; 16 //根据元素的第一个属性来判断当前的元素是不是目标元素 17 if (att.Value == strKey) 18 { 19 //对目标元素中的第二个属性赋值 20 att = nodes[i].Attributes["value"]; 21 att.Value = ConnenctionString; 22 break; 23 } 24 } 25 //保存上面的修改 26 doc.Save(strFileName); 27 System.Configuration.ConfigurationManager.RefreshSection("appSettings"); 28 }
现在回过头还是看上面的这个函数,看它的最后一行,它的作用是什么?
查找msdn文档可以发现微软出于性能考虑,对配置文件App.config采用了缓存策略,因此,尽管上面的函数确实在磁盘文件中修改了节点的值,当时,当用前面的那个函数读取的时候,会依然得到原来的那个值,仿佛没有修改一样!所以,必须使用这么句话,进行一遍刷新,强制要求程序下一次读取的时候,从磁盘文件读取!
好了,现在使用Visual Studio写C#程序的童鞋应该都遇到了一个蛋疼的问题,就是在debug时,明明在程序中修改了配置文件,可是下次重新执行程序的时候,发现程序根本没有发生变化,打开与exe文件相对应的config文件查看,发现文件根本没有变化!!!!明明就是当作xml文件来操作的,怎么会这样?!
其实这就涉及VS的运行机制问题了,细心的童鞋会在exe文件的同目录下,发现有一个与之对应的vshost.exe,以及vshost.exe.config 文件,当打开这里的这个config文件后会发现,在这里面的xml文件的值发生了变化!对滴~VS无论在Debug还是Release下,运行的程序都是这个带有vshost的程序,修改的也是这个程序对应的config。当时,当程序刚刚启动的时候,却是读取的原来与exe文件对应的config文件,将这个config文件内容替换原来与vshost.exe对应的config里面内容,这也就是为什么每次重新开程序后恢复原状的原因。
由于程序在VS里面调试的时候,运行的程序与直接去bin文件夹运行的程序不一样,所以,更推荐使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile来获取当前运行程序的配置文件。
当然啦,这一点差异不要着急,等程序调试完毕后,以后,程序一般就是从文件夹手动启动的啦,这个时候,就没有上面的那么多问题的了。一切会恢复正常!
5:app.config、exe.config和vshost.exe.config作用区别
vshost.exe.config是程序运行时的配置文本;
更多参考: