對(duì)于配置文件的常見操作包括:
l 讀取
l 修改
l 將web.config中的配置節(jié)放在單獨(dú)的文件中
l 對(duì)某一節(jié)進(jìn)行加密
l 添加定制的節(jié)
操作web配置文件(包括machine.config和web.config等)的命名空間是:System.Web.Configuration。主要應(yīng)用的類是:WebConfigurationManager。下面看看WebConfigurationManager類的成員。(可以利用MSDN來(lái)查看,我下面是利用Lutz Roeder的Reflector)
圖 1 WebConfigurationManager類成員
3.2.1 讀取
圖 2 WebConfigurationManager類中用于讀取的屬性和方法
3.2.1.1 讀取appSettings節(jié)和connectionStrings節(jié)
在WebConfigurationManager類中,我們首先注意到其兩個(gè)屬性AppSettings和ConnectionStrings。這兩個(gè)屬性就是用于操作我們前面看到的web.config文件中的兩節(jié)appSettings和connectionStrings。下面演示使用方法。
演示操作appSettings 節(jié)的代碼:
using System.Web.Configuration;
....
string message;
message = WebConfigurationManager.AppSettings["message"];
...
演示操作connectionStrings節(jié)的代碼:
using System.Web.Configuration;
....
string connectionString =
WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
...
3.2.1.2 讀取其它節(jié)
在.NET中讀取其它節(jié)要麻煩一些。我們需要利用GetSection()函數(shù)。GetSection()函數(shù)的作用是從當(dāng)前web應(yīng)用程序的配置文件中檢索指定的配置節(jié)。GetSection()函數(shù)有2個(gè)重載,我們這里介紹GetSection(string sectionName)。這個(gè)函數(shù)參數(shù)sectionName是用于查找節(jié)點(diǎn)的XPath表達(dá)式(對(duì)于XPath表達(dá)式的了解請(qǐng)參閱其它資料)。這里我們首先需要知道的是在.NET中所有配置節(jié)都對(duì)應(yīng)一個(gè)類,譬如上面我們看到的web.config文件中,appSettings節(jié)對(duì)應(yīng)于AppSettingsSection類,而connectionStrings對(duì)應(yīng)于ConnectionStringsSection類。利用Lutz Roeder的Reflector我們可以很容易的知道所有的section類。見下圖。
圖 3 繼承于ConfigurationSection類的各section類
從中我們查找到compilation節(jié)對(duì)應(yīng)于System.Web.Configuration. CompilationSection 類,authentication節(jié)對(duì)應(yīng)于System.Web.Configuration.AuthenticationSection類,identity節(jié)對(duì)應(yīng)于System.Web.Configuration.IdentitySection類,等等。
例子1:
protected void readImpersonationButton_Click(object sender, EventArgs e)
{
IdentitySection section;
section = WebConfigurationManager.GetSection("system.web/identity")
as IdentitySection;
if (section != null)
{
WriteMessage("Impersonate = " + section.Impersonate);
}
}
private void WriteMessage(string message)
{
//在頁(yè)面上添加一個(gè)名為messagePlaceHolder的PlaceHolder控件
HtmlGenericControl generic = new HtmlGenericControl();
generic.InnerHtml = message;
messagePlaceHolder.Controls.Add(generic);
}
上面例子中的XPath表達(dá)式system.web/identity相信大家都能猜出來(lái)是什么意思。而至于IdentitySection類到底有什么成員則需要利用Lutz Roeder的Reflector查找或MSDN查找。
例子2:(MSDN上的例子)此示例演示一個(gè)可從 Web 應(yīng)用程序或控制臺(tái)應(yīng)用程序訪問(wèn)的節(jié)。
// Show the use of GetSection(string).
// It gets the connectiobStrings section.
// If called from within a client application,
// the GetSection(string) gets the default connectionStrings
// section from the machine.config.
// If called from within a Web aplication it gets the
// section from the configuration file located at the
// application current level.
static void GetSection1()
{
// Get the connectionStrings section.
ConnectionStringsSection connectionStringsSection =
WebConfigurationManager.GetSection("connectionStrings")
as ConnectionStringsSection;
// Get the connectionStrings key,value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
connectionStringsSection.ConnectionStrings;
// Get the collection enumerator.
IEnumerator connectionStringsEnum =
connectionStrings.GetEnumerator();
// Loop through the collection and
// display the connectionStrings key, value pairs.
int i = 0;
Console.WriteLine("[Display the connectionStrings]");
while (connectionStringsEnum.MoveNext())
{
string name = connectionStrings[i].Name;
Console.WriteLine("Name: {0} Value: {1}",
name, connectionStrings[name]);
i += 1;
}
Console.WriteLine();
}
在MSDN上還有其它幾個(gè)例子可以參考。
3.2.2 修改
從圖2我們可以看到還有幾個(gè)靜態(tài)函數(shù)OpenWebConfiguration()、OpenMachineConfiguration()等,就是用來(lái)打開一個(gè)配置文件(分別對(duì)應(yīng)于web.config和machine.config)修改更新配置項(xiàng)的。
例子:把system.web項(xiàng)下的compilation修改(false->true;或true->false)。
protected void toggleDebugButton_Click(object sender, EventArgs e)
{
Configuration config;
config = WebConfigurationManager.OpenWebConfiguration("~");
CompilationSection compilation;
compilation = config.GetSection("system.web/compilation")
as CompilationSection;
if (compilation != null)
{
compilation.Debug = !compilation.Debug;
config.Save();
WriteMessage("Debug setting is now: " + compilation.Debug);
}
}
修改的注意點(diǎn):
1. 必須對(duì)該文件有修改的權(quán)限,通常NETWORK SERVICE和ASPNET賬戶對(duì)文件和目錄沒(méi)有修改權(quán)限。
2. ASP.NET在運(yùn)行時(shí),Runtime時(shí)刻注視著web.config文件的變化,一旦該文件修改了,整個(gè)應(yīng)用程序就創(chuàng)建一個(gè)新的實(shí)例,并重新加載。如果頻繁的修改web.config文件,對(duì)程序性能影響比較大。
3. 如果需要頻繁修改配置,則應(yīng)把該配置節(jié)放到單獨(dú)的文件中。
3.2.3 將web.config中的配置節(jié)放在單獨(dú)的文件中
我們可以把任意一個(gè)配置節(jié)(section)放入一個(gè)獨(dú)立的文件中。把web.config中的配置節(jié)放在單獨(dú)的文件中的優(yōu)點(diǎn),等一下再說(shuō)。先看看怎么做。采用本文開始的例子,分解如下。
例子:
web.config文件
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings configSource="appSettings.config"/>
<connectionStrings configSource="connections.config"/>
<system.web>
<compilation debug="true" />
<authentication mode="Windows"/>
<identity impersonate="true"/>
</system.web>
</configuration>
appSettings.config文件:
<appSettings>
<add key="message" value="Hello World!"/>
</appSettings>
connections.config文件:
<connectionStrings>
<add name="AdventureWorks" connectionString="..."/>
<add name="pubs" connectionString="..."/>
</connectionStrings>
利用外部配置文件的好處:
1. 可以根據(jù)不同的環(huán)境而設(shè)置不同的配置,譬如開發(fā)環(huán)境、測(cè)試環(huán)境、正式環(huán)境配置不同的數(shù)據(jù)庫(kù)連接。
2. 權(quán)限管理。例如,可以鎖住web.config不讓某些人修改,而只讓其用于修改appSettings.config文件的權(quán)限。
3. 利用外部配置文件,還可以控制當(dāng)其修改后應(yīng)用程序是否重啟。一般,當(dāng)我們修改了web.config文件時(shí),應(yīng)用程序就產(chǎn)生一個(gè)新的實(shí)例并重啟。而如果是單獨(dú)的配置文件,則可以控制重啟與否。
配置方法:在machine.config中配置。Machine.config文件一般在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG目錄下。打開machine.config文件,截圖如下:
圖 4 machine.config文件內(nèi)容截圖
從圖4我們可以看到在有的元素中有restartOnExternalChanges屬性。如果設(shè)為false,則外部文件修改后不重啟,如果設(shè)為true,則重啟。
3.2.4 對(duì)某一節(jié)進(jìn)行加密
在.NET2.0中加密某一節(jié)(section),那是相當(dāng)?shù)暮?jiǎn)單。
在配置文件中,有些信息是不希望別人看到的。例如在<connectionStrings>節(jié)中可能包含連接數(shù)據(jù)庫(kù)的用戶名和密碼。<identity>節(jié)中也可能包含用戶名和密碼。
注意:有些節(jié)可能含有密碼,但不能加密,例如<processModel>節(jié),為了安全,你可以應(yīng)用Aspnet_setreg.exe工具來(lái)保存密碼。
下面的例子演示如何加密和解密一節(jié)。提醒,這里的加密和解密用的單詞是protect和unprotect。注意:在實(shí)際應(yīng)用中,我們并不需要明確的調(diào)用解密函數(shù)。在運(yùn)行市,程序讀取到加密了的配置節(jié)時(shí)將會(huì)自動(dòng)調(diào)用解密。
protected void toggleEncryptionButton_Click(object sender, EventArgs e)
{
Configuration config;
config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section;
section = config.GetSection("connectionStrings")
as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
}
else
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
config.Save();
WriteMessage("connections protected = " + section.SectionInformation.IsProtected);
}
此代碼的作用就是加密和解密connectionStrings節(jié)。如果原來(lái)加密(通過(guò)屬性IsProtected判斷)了,則解密。否則相反。分別調(diào)用了UnprotectSection()和protectSection()函數(shù)。
下面是相關(guān)文件的變化。
加密前的connectionStrings.config文件內(nèi)容如下:
圖 5 加密前的connectionStrings.config文件內(nèi)容
加密后connectionStrings.config文件內(nèi)容如下:
圖 6 加密后connectionStrings.config文件內(nèi)容
對(duì)于結(jié)果的改變,我們可以對(duì)比一下,自己了解其變化。
但在上面的代碼片斷中,我們還有一個(gè)地方需要了解:加密提供者。目前有兩種加密提供者:DataProtectionConfigurationProvider 和 RSAProtectedConfigurationProvider。其中DataProtectionConfigurationProvider是利用Windows Data Protection API(DPAPI)提供與機(jī)器相關(guān)的加密解密。也就是說(shuō)我們?cè)谀囊慌_(tái)機(jī)器上加密就只能在那一臺(tái)機(jī)器上解密。如果需要在其它機(jī)器上解密則只能使用DataProtectionConfigurationProvider。
3.2.5添加定制的配置節(jié)
當(dāng)我們需要配置的數(shù)據(jù)比較多的時(shí)候,為了管理的方便。我們可能需要添加定制的配置節(jié)。下面用一個(gè)比較簡(jiǎn)單的例子來(lái)演示。
<configuration>
<configSections>
<section name="Logging" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Logging>
<add key="File Name" value="MyApp.log" />
<add key="MessageColumns" value="5" />
<add key="MaxFileSize" value="40000000000000" />
</Logging>
</configuration>
聯(lián)系客服