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返回的類型不同,具體獲得配置內容的方式也不一樣。 配置節處理程序
返回類型
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
新聞熱點
疑難解答