当前位置: 移动技术网 > IT编程>开发语言>.net > 自定义应用程序配置文件(app.config)

自定义应用程序配置文件(app.config)

2018年04月25日  | 移动技术网IT编程  | 我要评论

上海东明家具城官网,uc松鼠大战辅助,58挂靠网

1. 配置文件概述: 
应用程序配置文件是标准的 xml 文件,xml 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是configuration。我们经常访问的是appsettings,它是由.net预定义配置节。我们经常使用的配置文件的架构是象下面的形式。先大概有个印象,通过后面的实例会有一个比较清楚的认识。下面的“配置节”可以理解为进行配置一个xml的节点。

常见配置文件模式:

<configuration>
<configsections> //配置节声明区域,包含配置节和命名空间声明
<section> //配置节声明
  <sectiongroup> //定义配置节组
   <section> //配置节组中的配置节声明
<appsettings> //预定义配置节
<custom element for configuration section> //配置节设置区域

2. 只有appsettings节的配置文件及访问方法 

下面是一个最常见的应用程序配置文件的例子,只有appsettings节。
程序代码:    [ 复制代码到剪贴板 ]
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appsettings>
<add key="connectionstring" value="user id=sa;data source=.;password=;initial catalog=test;provider=sqloledb.1;" />
<add key="templatepath" value="template" />
</appsettings>
</configuration>


下面来看看这样的配置文件如何方法。

程序代码:    [ 复制代码到剪贴板 ]
string _connectionstring=configurationsettings.appsettings["connectionstring"];

使用configurationsettings类的静态属性appsettings就可以直接方法配置文件中的配置信息。这个属性的类型是namevaluecollection。

3. 自定义配置文件 

3.1 自定义配置节

一个用户自定义的配置节,在配置文件中分为两部分:一是在<configsections></ configsections> 配置节中声明配置节(上面配置文件模式中的“<section>”),另外是在<configsections>< / configsections >之后设置配置节(上面配置文件模式中的“< custom element for configuration section>”),有点类似一个变量先声明,后使用一样。声明一个配置文件的语句如下:

<section name=" " type=" "/> 
<section>:声明新配置节,即可创建新配置节。

name:自定义配置节的名称。

type:自定义配置节的类型,主要包括system.configuration.singletagsectionhandler、 system.configuration.dictionarysectionhandler、 system.configuration.namevaluesectionhandler。

不同的type不但设置配置节的方式不一样,最后访问配置文件的操作上也有差异。下面我们就举一个配置文件的例子,让它包含这三个不同的type。

程序代码:
<?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>


我们对上面的自定义配置节进行说明。在声明部分使用<section name="test1" type= "system.configuration.singletagsectionhandler"/>声明了一个配置节它的名字叫test1,类型为singletagsectionhandler。在设置配置节部分使用 <test1 setting1= "hello" setting2="world"/>设置了一个配置节,它的第一个设置的值是hello,第二个值是world,当然还可以有更多。其它的两个配置节和这个类似。 
下面我们看在程序中如何访问这些自定义的配置节。我们用过configurationsettings类的静态方法getconfig来获取自定义配置节的信息。

程序代码:   public static object getconfig(string sectionname);


下面是访问这三个配置节的代码:

程序代码: 
//访问配置节test1
idictionary idtest1 = (idictionary)configurationsettings.getconfig("test1");
string str = (string)idtest1["setting1"] +" "+(string)idtest1["setting2"];
messagebox.show(str); //输出hello world

//访问配置节test1的方法2
string[] values1=new string[idtest1.count];
idtest1.values.copyto(values1,0);
messagebox.show(values1[0]+" "+values1[1]); //输出hello world

//访问配置节test2
idictionary idtest2 = (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);
messagebox.show(keys[0]+" "+values[0]);

//访问配置节test3
namevaluecollection nc=(namevaluecollection)configurationsettings.getconfig("test3");
messagebox.show(nc.allkeys[0].tostring()+" "+nc["hello"]); //输出hello world


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

[table]
配置节处理程序|返回类型[br]
[/table]
singletagsectionhandler systems.collections.idictionary

dictionarysectionhandler systems.collections.idictionary

namevaluesectionhandler systems.collections.specialized.namevaluecollection


3.2 自定义配置节组 
配置节组是使用<sectiongroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的包含元素,在< configsections>元素中声明配置节组,并将属于该组的节置于<sectiongroup>元素中。下面是一个包含配置节组的配置文件的例子:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<configsections> 
<sectiongroup name="testgroup"> 
<section name="test" type="system.configuration.namevaluesectionhandler"/> 
</sectiongroup> 
</configsections> 

<testgroup> 
<test> 
<add key="hello" value="world"/> 
</test> 
</testgroup> 
</configuration> 


下面是访问这个配置节组的代码:

程序代码:
namevaluecollection nc=(namevaluecollection)configurationsettings.getconfig("testgroup/test");
messagebox.show(nc.allkeys[0].tostring()+" "+nc["hello"]); //输出hello world 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网