原文:http://www.cnblogs.com/dflying/archive/2006/04/17/377276.htmlhtml
本文翻譯整理自Carlos Aguilar Mares的blog文章:Microsoft.Web.Administration in IIS 7。web
請注意本文的內容均基於Windows Vista Beta 2版本,在正式的發佈版本中可能會有所改變。編程
Microsoft中提供了管理IIS7的一些很是強大的API——Microsoft.Web.Administration,能夠很方便的讓咱們以編程的方式管理,設定IIS 7的各項配置。Microsoft.Web.Administration.dll位於IIS的目錄(%WinDir%\System32\InetSrv)下,在項目中添加對其的引用後您就能夠使用這些API了。下圖顯示了Microsoft.Web.Administration.dll中的主要對象。app
讓咱們經過幾個例子來使用Microsoft.Web.Administration,下面的例子均很是易懂,我就再也不過多解釋了。ui
創建一個站點(Site)spa
ServerManager iisManager
=
new
ServerManager();
iisManager.Sites.Add(
"
NewSite
"
,
"
http
"
,
"
*:8080:
"
,
"
d:\\MySite
"
);
iisManager.Update();
將一個應用程序(Application)添加到一個站點翻譯
ServerManager iisManager
=
new
ServerManager();
iisManager.Sites[
"
NewSite
"
].Applications.Add(
"
/Sales
"
,
"
d:\\MyApp
"
);
iisManager.Update();
創建一個虛擬目錄(Virtual Directory)htm
ServerManager iisManager
=
new
ServerManager();
Application app
=
iisManager.Sites[
"
NewSite
"
].Applications[
"
/Sales
"
];
app.VirtualDirectories.Add(
"
/VDir
"
,
"
d:\\MyVDir
"
);
iisManager.Update();
運行時控制:中止一個站點對象
ServerManager iisManager
=
new
ServerManager();
iisManager.Sites[
"
NewSite
"
].Stop();
運行時控制:回收應用程序池(Recyciling an Application Pool)blog
ServerManager iisManager
=
new
ServerManager();
iisManager.ApplicationPools[
"
DefaultAppPool
"
].Recycle();
運行時控制:獲得當前正在處理的請求
ServerManager iisManager
=
new
ServerManager();
foreach
(WorkerProcess w3wp
in
iisManager.WorkerProcesses) {
Console.WriteLine(
"
W3WP ({0})
"
, w3wp.ProcessId);
foreach
(Request request
in
w3wp.GetRequests(
0
)) {
Console.WriteLine(
"
{0} - {1},{2},{3}
"
,
request.Url,
request.ClientIPAddr,
request.TimeElapsed,
request.TimeInState);
}
}
還有一個有用之處是Microsoft.Web.Administration提供了對*.config(例如web.config)文件的編輯功能,讓您不再用像修改普通XML文件同樣修改*.config了。可是原做者說他將在從此的Post中詳細說明,我也只暫且寫到這裏了。