C#读取配置文件浅析(转自:http://developer.51cto.com/art/200908/143706.htm)

C#读取配置文件是如何实现的呢?本文就C#读取配置文件方面向你介绍相关内容。希望本文能对大家有所帮助。

AD:

C#读取配置文件是如何实现的呢?在.Net中提供了配置文件,让我们可以很方面的处理配置信息,这个配置是XML格式的。而且.Net中已经提供了一些访问这个文件的功能。

C#读取配置文件1、读取配置信息

下面是一个配置文件的具体内容:

 
  
  1. <add span=""><span class="string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"coal"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">value=</span><span class="string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"一二三"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">/&gt; </span></add>
  2. <add span=""><span class="string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"inWellTime"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">value=</span><span class="string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"5"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">/&gt;</span></add>

.Net提供了可以直接访问(注意大小写)元素的方法,在这元素中有很多的子元素,这些子元素名称都是“add”,有两个属性分别是“key”和“value”。一般情况下我们可以将自己的配置信息写在这个区域中,通过下面的方式进行访问:

 
  
  1. StringConString=System.Configuration.ConfigurationSettings.AppSettings["inWellTime"];

在AppSettings后面的是子元素的key属性的值,例如AppSettings["inWellTime"],我们就是访问这个子元素,它的返回值就是“5”,即value属性的值。

C#读取配置文件2、设置配置信息

如果配置信息是静态的,我们可以手工配置,要注意格式。如果配置信息是动态的,就需要我们写程序来实现。在.Net中没有写配置文件的功能,我们可以使用操作XML文件的方式来操作配置文件。

写了个WinForm中读写配置文件App.config的类

C#读取配置文件代码如下:

 
  
  1. usingSystem;
  2. usingSystem.Configuration;
  3. usingSystem.Xml;
  4. usingSystem.Data;
  5. namespacecn.zhm.common
  6. {
  7. ///
  8. ///ConfigClass的摘要说明。
  9. ///
  10. publicclassConfigClass
  11. {
  12. publicstringstrFileName;
  13. publicstringconfigName;
  14. publicstringconfigValue;
  15. publicConfigClass()
  16. {
  17. //
  18. //TODO:在此处添加构造函数逻辑
  19. //
  20. }
  21. publicstringReadConfig(stringconfigKey)
  22. {
  23. configValue="";
  24. configValue=ConfigurationSettings.AppSettings[""+configKey+""];
  25. returnconfigValue;
  26. }
  27. //得到程序的config文件的名称以及其所在的全路径
  28. publicvoidSetConfigName(stringstrConfigName)
  29. {
  30. configName=strConfigName;
  31. //获得配置文件的全路径
  32. GetFullPath();
  33. }
  34. publicvoidGetFullPath()
  35. {
  36. //获得配置文件的全路径
  37. strFileName=AppDomain.CurrentDomain.BaseDirectory.ToString()+configName;
  38. }
  39. publicvoidSaveConfig(stringconfigKey,stringconfigValue)
  40. {
  41. XmlDocumentdoc=newXmlDocument();
  42. doc.Load(strFileName);
  43. //找出名称为“add”的所有元素
  44. XmlNodeListnodes=doc.GetElementsByTagName("add");
  45. for(inti=0;i{
  46. //获得将当前元素的key属性
  47. XmlAttributeatt=nodes[i].Attributes["key"];
  48. //根据元素的第一个属性来判断当前的元素是不是目标元素
  49. if(att.Value==""+configKey+"")
  50. {
  51. //对目标元素中的第二个属性赋值
  52. att=nodes[i].Attributes["value"];
  53. att.Value=configValue;
  54. break;
  55. }
  56. }
  57. //保存上面的修改
  58. doc.Save(strFileName);
  59. }
  60. }
  61. }

C#读取配置文件应用如下:

C#读取配置文件之读取:

 
  
  1. ConfigClassconfig=newConfigClass();
  2. stringcoal=config.ReadConfig("coal");
  3. this.tbOpenFile.Text=config.ReadConfig("inWellTime");

C#读取配置文件之写:

 
  
  1. ConfigClassconfig=newConfigClass();
  2. //得到程序的config名:DataOperate.exe.config;
  3. config.SetConfigName("DataOperate.exe.config");
  4. config.SaveConfig("coal","三二一");
  5. config.SaveConfig("inWellTime","10");

注意:当修改完App.config。文件后,程序中用到的App.config文件的“key”对应的“value”值需要重读,否则修改后修改并不能立即起作用,而要等下次程序重启后才可以读取到修改后的App.config属性值。

C#读取配置文件的相关内容就向你介绍到这里,希望对你学习C#读取配置文件有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值