C#修該web.config文件中的配置項

在ASP.NET2.0裏不但進一步擴展了配置文件web.config,更爲重要的是系統提供了一組API函數,讓我們可以以編程 的方式從配置文件裏提取信息
    首先,先看看如果從web.config裏提取appSettings裏的配置值,示例代碼如下:
 <appSettings>
       <add key="pagetitle" value="http://blog.netbei.com/Job Site Starter Kit (Ver.1.0)"></add>
        <add key="sitelogo" value="logo.gif"></add>
        <add key="advertiseemail" value="[email protected]"></add >
     </appSettings>
利用ASP.NET2.0提供的一組API函數,您可以很容易的獲取AppSettingsSection裏所有的Keys/value組對,如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings");
string[] appKeys = appSettings.Settings.AllKeys;
for (int i = 0; i < appSettings.Settings.Count; i++)
{
//這裏只進行簡單的輸出
Response.Write(appSettings.Settings[appKeys[i]].Value);
Response.Write("<BR>");
}

上面代碼只是進行簡單的輸出所有Key的value值,然而,你可能想獲取的僅僅是某一個key的值,這也非常簡單,如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings")
 string pateTitle= appSettings.Settings["pagetitle"].Value; //獲取key爲patetitle的value值
string siteLogo= appSettings.Settings["siteLogo"].Value; //獲取key爲sitelogo的value值

對於數據庫連接字符串,在ASP.NET2.0裏提供了專門的配置節如下:
<connectionStrings>
    <add name="connectionstring"
connectionString="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/>
<add name="MyProviderConnectionString"
connectionString="Data Source=SQLEXPRESS;Integrated Security=True;  … …"/>
</connectionStrings>

這樣我們很容易獲取數據庫連接字符串如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionstring ");

ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
foreach (ConnectionStringSettings conSetting in conCollection)
{

Response.Write(conSetting.ConnectionString);

Response.Write("<BR>");

}

另外,利用API函數,你同時還可以在代碼裏更改web.config數據庫連接的配置的值,如下
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringsSection conSection= (ConnectionStringsSection)config.GetSection("connectionStrings");
conSection.ConnectionStrings["SQLConnectionString"].ConnectionString =
"Data Source=SQLEXPRESS;Integrated Security=True;  … …";
config.Save();

這裏最有意思的可能就是類的轉換,在<appSettings ></appSettings>裏,使用的是AppSettingsSection類, 在<connectionStrings></ connectionStrings>裏使用的的是ConnectionStringsSection類,事實上,ASP.NET2.0提供的一組 函數都是“配置節名+Section”的形式提供的類。

   在ASP.NET官方網站曾經對此專門介紹,可以找不到該文件了。

在ASP.NET2.0裏提供了兩種方式對數據庫連接字符串加密,一種是使用asp_regii命令,一種是通過代碼,下面顯示的是通過代碼方式對數據庫連接字符串加密,代碼如下:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSection = config.GetSection("connectionStrings");
if (configSection.SectionInformation.IsProtected)

{//如果已經加密,就不用再加密了

configSection.SectionInformation.UnprotectSection();
config.Save();
}
else
{
configSection.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider");

config.Save();

}

這樣,你檢查該文件的配置可能如下:

<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">

<EncryptedData>

<CipherData>

<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVClqG40BZkCjK40

adynN8gQAAAACAAAAAAADZgAAqAAAABAAAABIhtOW …PE

</CipherData>

</EncryptedData>

</connectionStrings>

轉自:http://dev.tot.name/csharp/html/20100214/20100214214420.htm

發佈了45 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章