.NET读取config配置文件方法的总结——有疑求解?

概述

1,在MSDN中对于配置文件的定义:

   配置文件是可以按需要更改的 XML 文件。 开发人员可以使用配置文件来更改设置,而不必重编译应用程序。 管理员可以使用配置文件来设置策略,以便影响应用程序在计算机上运行的方式。

   在我们编程的时候经常有一些字符串需要改变,例如:数据库连接字符串,设置分页时每页的记录数量。这时候我们就不能再去打开程序,或者找到cs或者aspx文件,然后记事本打开,再改写为自己需要的值。就可达到预期的效果。

2,配置文件的格式及模式:

是.Net自带的两种Winform中是app.config,web中是Web.config。

在config配置文件中。.Net framework 已经为它规定了一定的模式如下:

<configuration>
  <configSections><!--配置节声明区域,包括配置节和命名空间声明-->
    <section name="" type=""/><!--配置节声明-->
  </configSections>
  <appSettings>
    <add key="" value=""/><!--预定义配置节-->
  </appSettings>
</configuration>
3,配置文件中appSettings节的配置文件及访问方法
<configuration>
  <appSettings>
    <add key="SQLCONNECTIONSTRING" value="Data Source=sjk;DataBase=sjk;UID=sa;PWD=123"/>
  </appSettings>
</configuration>
在后台读取配置节中的字符串时有多种读法
简单的是调用System.Configuration后,直接读取(这种方法只可以读取)
然后string str=ConfigurationSettings.AppSettings["strconn"]
当然也可以将配置文件转化为xml文档,然后读取
 XmlDocument doc = new XmlDocument();
                string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "App.config";
                doc.Load(strFileName);
                XmlNodeList nodes = doc.GetElementsByTagName("add");
                for (int i = 0; i < nodes.Count; i++)
                {
                    XmlAttribute att = nodes[i].Attributes["key"];
                    if (att.Value == "SQLCONNECTIONSTRING")
                    {
                        att = nodes[i].Attributes["value"];
                        string str=att.Value;
                        break;
                    }
                }

根据xml文档的节点是否是和设定的一样,读取配置文件。

4,自定义配置节的配置和读取方式(转自百度知道)

 <configSections>
        <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
    </configSections>

section:声明新配置节,可创建配置节。name:自定义配置节的名称。type:自定义配置节的类型。主要包括一下三种:

System.Configuration.SingleTagSectionHandler

System.Configuration.DictionarySectionHandler

System.Configuration.NameValueSectionHandler

下面看个例子:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
        <section name="Test2" type="System.Configuration.DictionarySectionHandler"/>
        <section name="Test3" type="System.Configuration.NameValueSectionHandler" />
    </configSections>
    
    <Test1 setting1="Hello" setting2="World"/>
    <Test2>
        <add key="Hello" value="World" />
    </Test2>
    <Test3>
        <add key="Hello" value="World" />
    </Test3>    
</configuration>
 

下面是访问这三个配置节

 //访问配置节Test1的方法1
             System.Collections.IDictionary IDTest1 = (System.Collections.IDictionary)ConfigurationSettings.GetConfig("Test1");
             label1.Text = (string)IDTest1["setting1"];
             label2.Text = (string)IDTest1["setting2"];
             //访问配置节Test1的方法2
            string[] values1=new string[IDTest1.Count];
            IDTest1.Values.CopyTo(values1,0);
            label3.Text = values1[1].ToString();
            label4.Text = values1[0].ToString();  
            //访问配置节Test2
            System.Collections.IDictionary IDTest2 = (System.Collections.IDictionary)ConfigurationSettings.GetConfig("Test2");
            string[] keys=new string[IDTest2.Keys.Count];
            string[] values=new string[IDTest2.Keys.Count];
            IDTest2.Keys.CopyTo(keys,0);
            IDTest2.Values.CopyTo(values,0);
            label5.Text = keys[0].ToString();
            label6.Text = values[0].ToString();

            //访问配置节Test3  
            System.Collections.Specialized.NameValueCollection nc=(System.Collections.Specialized.NameValueCollection)ConfigurationSettings.GetConfig("Test3");
            label7.Text = nc.AllKeys[0].ToString();
            label8.Text = "World";  //输出Hello World

通过上面的代码我们可以看出,不同的type通过GetConfig返回的类型不同,具体获得配置内容的方式也不一样。

 

只知其然,不知其所以然。求答案。

上面是我写的程序。然后有很多疑问:

1,对于访问配置节1的第二种方法,

label3.Text = values1[1].ToString();

label4.Text = values1[0].ToString();

读取数组的第二个是Hello,数组的第一个是World,有些搞不懂,难道将IDTest1复制到values1是反正复制的?

测试也是按上面的程序才对。求解?

2,配置节2中

IDTest2.Keys.CopyTo(keys,0);

IDTest2.Values.CopyTo(values,0);

都是将IDTest2复制到数组中,怎么keys和values不一样呢?求解

3,配置节3中label7.Text = nc.AllKeys[0].ToString()获取的是所有key的值,只取得Hello可以理解

问一下有没办法取得value值

转载于:https://www.cnblogs.com/love-liu/archive/2011/12/29/2306524.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值