對于配置文件的常見(jiàn)操作包括:
l 讀取
l 修改
l 將web.config中的配置節放在單獨的文件中
l 對某一節進(jìn)行加密
l 添加定制的節
操作web配置文件(包括machine.config和web.config等)的命名空間是:System.Web.Configuration。主要應用的類(lèi)是:WebConfigurationManager。下面看看WebConfigurationManager類(lèi)的成員。(可以利用MSDN來(lái)查看,我下面是利用Lutz Roeder的Reflector)
圖 1 WebConfigurationManager類(lèi)成員
3.2.1 讀取
圖 2 WebConfigurationManager類(lèi)中用于讀取的屬性和方法
3.2.1.1 讀取appSettings節和connectionStrings節
在WebConfigurationManager類(lèi)中,我們首先注意到其兩個(gè)屬性AppSettings和ConnectionStrings。這兩個(gè)屬性就是用于操作我們前面看到的web.config文件中的兩節appSettings和connectionStrings。下面演示使用方法。
演示操作appSettings 節的代碼:
using System.Web.Configuration;
....
string message;
message = WebConfigurationManager.AppSettings["message"];
...
演示操作connectionStrings節的代碼:
using System.Web.Configuration;
....
string connectionString =
WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
...
3.2.1.2 讀取其它節
在.NET中讀取其它節要麻煩一些。我們需要利用GetSection()函數。GetSection()函數的作用是從當前web應用程序的配置文件中檢索指定的配置節。GetSection()函數有2個(gè)重載,我們這里介紹GetSection(string sectionName)。這個(gè)函數參數sectionName是用于查找節點(diǎn)的XPath表達式(對于XPath表達式的了解請參閱其它資料)。這里我們首先需要知道的是在.NET中所有配置節都對應一個(gè)類(lèi),譬如上面我們看到的web.config文件中,appSettings節對應于A(yíng)ppSettingsSection類(lèi),而connectionStrings對應于ConnectionStringsSection類(lèi)。利用Lutz Roeder的Reflector我們可以很容易的知道所有的section類(lèi)。見(jiàn)下圖。
圖 3 繼承于ConfigurationSection類(lèi)的各section類(lèi)
從中我們查找到compilation節對應于System.Web.Configuration. CompilationSection 類(lèi),authentication節對應于System.Web.Configuration.AuthenticationSection類(lèi),identity節對應于System.Web.Configuration.IdentitySection類(lèi),等等。
例子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表達式system.web/identity相信大家都能猜出來(lái)是什么意思。而至于IdentitySection類(lèi)到底有什么成員則需要利用Lutz Roeder的Reflector查找或MSDN查找。
例子2:(MSDN上的例子)此示例演示一個(gè)可從 Web 應用程序或控制臺應用程序訪(fǎng)問(wèn)的節。
// 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)函數OpenWebConfiguration()、OpenMachineConfiguration()等,就是用來(lái)打開(kāi)一個(gè)配置文件(分別對應于web.config和machine.config)修改更新配置項的。
例子:把system.web項下的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. 必須對該文件有修改的權限,通常NETWORK SERVICE和ASPNET賬戶(hù)對文件和目錄沒(méi)有修改權限。
2. ASP.NET在運行時(shí),Runtime時(shí)刻注視著(zhù)web.config文件的變化,一旦該文件修改了,整個(gè)應用程序就創(chuàng )建一個(gè)新的實(shí)例,并重新加載。如果頻繁的修改web.config文件,對程序性能影響比較大。
3. 如果需要頻繁修改配置,則應把該配置節放到單獨的文件中。
3.2.3 將web.config中的配置節放在單獨的文件中
我們可以把任意一個(gè)配置節(section)放入一個(gè)獨立的文件中。把web.config中的配置節放在單獨的文件中的優(yōu)點(diǎn),等一下再說(shuō)。先看看怎么做。采用本文開(kāi)始的例子,分解如下。
例子:
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. 可以根據不同的環(huán)境而設置不同的配置,譬如開(kāi)發(fā)環(huán)境、測試環(huán)境、正式環(huán)境配置不同的數據庫連接。
2. 權限管理。例如,可以鎖住web.config不讓某些人修改,而只讓其用于修改appSettings.config文件的權限。
3. 利用外部配置文件,還可以控制當其修改后應用程序是否重啟。一般,當我們修改了web.config文件時(shí),應用程序就產(chǎn)生一個(gè)新的實(shí)例并重啟。而如果是單獨的配置文件,則可以控制重啟與否。
配置方法:在machine.config中配置。Machine.config文件一般在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG目錄下。打開(kāi)machine.config文件,截圖如下:
圖 4 machine.config文件內容截圖
從圖4我們可以看到在有的元素中有restartOnExternalChanges屬性。如果設為false,則外部文件修改后不重啟,如果設為true,則重啟。
3.2.4 對某一節進(jìn)行加密
在.NET2.0中加密某一節(section),那是相當的簡(jiǎn)單。
在配置文件中,有些信息是不希望別人看到的。例如在<connectionStrings>節中可能包含連接數據庫的用戶(hù)名和密碼。<identity>節中也可能包含用戶(hù)名和密碼。
注意:有些節可能含有密碼,但不能加密,例如<processModel>節,為了安全,你可以應用Aspnet_setreg.exe工具來(lái)保存密碼。
下面的例子演示如何加密和解密一節。提醒,這里的加密和解密用的單詞是protect和unprotect。注意:在實(shí)際應用中,我們并不需要明確的調用解密函數。在運行市,程序讀取到加密了的配置節時(shí)將會(huì )自動(dòng)調用解密。
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節。如果原來(lái)加密(通過(guò)屬性IsProtected判斷)了,則解密。否則相反。分別調用了UnprotectSection()和protectSection()函數。
下面是相關(guān)文件的變化。
加密前的connectionStrings.config文件內容如下:
圖 5 加密前的connectionStrings.config文件內容
加密后connectionStrings.config文件內容如下:
圖 6 加密后connectionStrings.config文件內容
對于結果的改變,我們可以對比一下,自己了解其變化。
但在上面的代碼片斷中,我們還有一個(gè)地方需要了解:加密提供者。目前有兩種加密提供者:DataProtectionConfigurationProvider 和 RSAProtectedConfigurationProvider。其中DataProtectionConfigurationProvider是利用Windows Data Protection API(DPAPI)提供與機器相關(guān)的加密解密。也就是說(shuō)我們在哪一臺機器上加密就只能在那一臺機器上解密。如果需要在其它機器上解密則只能使用DataProtectionConfigurationProvider。
3.2.5添加定制的配置節
當我們需要配置的數據比較多的時(shí)候,為了管理的方便。我們可能需要添加定制的配置節。下面用一個(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)系客服