C# 读取配置文件和修改(二)

读取:

[csharp]  view plain  copy
  1. string str= ConfigurationManager.AppSettings["key"];  

写入:

[csharp]  view plain  copy
  1.  Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  2.  configuration.AppSettings.Settings[key].Value = result;  
  3. configuration.Save();  
  4. ConfigurationManager.RefreshSection("appSettings");  

在VS 2005中设置和读取配置文件已经变的很简单了,而且是强类型的,读取的值可以直接赋值给相应的变量,无需强制转换。

例如:DateTime userDateTime1 = Properties.Settings.Default.userDateTime1; 真是方便了很多。但是你有没有发现,使用 Properties.Settings.Default.Save() 保存了设置后,Application 范围的设置为什么没有保存成功,User 范围的设置的变化为什么没有体现到 app.config 文件中去呢? 

1. 在VS 2005中进行应用程序设置
    打开 项目属性 ? 设置,如下图:


 输入名称,选择类型和范围,输入值保存即完成设置。
   类型:int,string,DateTime等各种数据类型;
   范围:Application  范围的设置对所有用户都有效;
           User 范围的设置对当前用户(当前 Windows 登录的用户)有效,同一个设置每个用户可以有不同的值,而且互不影响。

2. 读取配置文件(读取应用程序设置)
     无论是Application  范围的设置,还是User 范围的设置,读取的方法都是一样的。

[csharp]  view plain  copy
  1. // 读取设置  
  2.    this.appSetting1TextBox.Text = Properties.Settings.Default.appSetting1;  
  3.    this.userSetting1TextBox.Text = Properties.Settings.Default.userSetting1;  

3. 保存 User 范围配置文件(保存 User 范围的应用程序设置)    
[csharp]  view plain  copy
  1. // 保存 User 范围的设置  
  2.    Properties.Settings.Default.userSetting1 = this.userSetting1TextBox.Text;  
  3.    Properties.Settings.Default.Save();  

 User 范围配置文件没有保存在应用程序文件夹下,而是保存在这里:X:\Documents and Settings\Windows登录用户\Local Settings\Application Data。
4. 保存 Application 范围配置文件(保存 Application 范围的应用程序设置)     
    保存 Application 范围配置文件可没有保存 User 范围配置文件那样简单,直接 Properties.Settings.Default.Save() 是不行的。因为 Application 范围的设置在运行时是“只读”的。这里使用的方法是使用 XmlDocument 来直接保存 config 文件,然后在 Reload 设置。
[csharp]  view plain  copy
  1. // 保存 Applicationi 范围的设置  
  2.    string configFileName = Application.ExecutablePath + ".config";  
  3.    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();  
  4.    doc.Load(configFileName);  
  5.    string configString = @"configuration/applicationSettings/SetConfig.Properties.Settings/setting[@name='appSetting1']/value";  
  6.    System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);  
  7.    if (configNode != null)  
  8.    {  
  9.        configNode.InnerText = this.appSetting1TextBox.Text;  
  10.        doc.Save(configFileName);  
  11.        // 刷新应用程序设置,这样下次读取时才能读到最新的值。  
  12.        Properties.Settings.Default.Reload();  
  13.    }  

顺便说一下:使用 Properties.Settings.Default.Reset() 可以恢复 User 范围设置的默认值(从 app.config 中恢复)。

这下面两个方法也类似,摘抄下来做个备份学习:


[csharp]  view plain  copy
  1. private void SaveConfig(string ConnenctionString)  
  2. {  
  3. XmlDocument doc=new XmlDocument();  
  4. //获得配置文件的全路径  
  5. string strFileName=AppDomain.CurrentDomain.BaseDirectory.ToString()+".exe.config";  
  6. doc.Load(strFileName);  
  7. //找出名称为“add”的所有元素  
  8. XmlNodeList nodes=doc.GetElementsByTagName("add");  
  9. for(int i=0;i<nodes.Count;i++)  
  10. {  
  11. //获得将当前元素的key属性  
  12. XmlAttribute att=nodes[i].Attributes["key"];  
  13. //根据元素的第一个属性来判断当前的元素是不是目标元素  
  14. if (att.Value=="ConnectionString")   
  15. {  
  16. //对目标元素中的第二个属性赋值  
  17. att=nodes[i].Attributes["value"];  
  18. att.Value=ConnenctionString;  
  19. break;  
  20. }  
  21. }  
  22. //保存上面的修改  
  23. doc.Save(strFileName);  
  24. }   
  25. ==============================================================================  
  26. public static void SetValue(string AppKey,string AppValue)  
  27. {  
  28. XmlDocument xDoc = new XmlDocument();  
  29. xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");  
  30.   
  31. XmlNode xNode;  
  32. XmlElement xElem1;  
  33. XmlElement xElem2;  
  34. xNode = xDoc.SelectSingleNode("//appSettings");  
  35.   
  36. xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");  
  37. if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);  
  38. else  
  39. {  
  40. xElem2 = xDoc.CreateElement("add");  
  41. xElem2.SetAttribute("key",AppKey);  
  42. xElem2.SetAttribute("value",AppValue);  
  43. xNode.AppendChild(xElem2);  
  44. }  
  45. xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");  
  46. }  

猜你喜欢

转载自blog.csdn.net/lbc2100/article/details/79639355