国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義應用程序配置文件(app.config)

2019-11-18 17:56:47
字體:
來源:轉載
供稿:網友

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;        <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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨眉山市| 宝丰县| 平罗县| 博野县| 隆尧县| 屯门区| 杭州市| 涟源市| 信宜市| 西畴县| 伊金霍洛旗| 宁陕县| 冕宁县| 闸北区| 依兰县| 佛坪县| 济南市| 建平县| 虹口区| 开平市| 石嘴山市| 伊宁市| 富川| 斗六市| 金平| 靖边县| 麟游县| 泽普县| 武山县| 德阳市| 望江县| 杨浦区| 扎囊县| 日照市| 德令哈市| 福建省| 澄迈县| 谢通门县| 额敏县| 夏邑县| 海城市|