程式中需USE COMOBJ單元 1.Q:如何得到機器上IIS中所有的WEB虛擬站點. A: var InstallPath: String; WebSite, WebServer, WebRoot: Variant; count: Integer; Flag: Boolean; begin Fla ...
程式中需USE COMOBJ單元
1.Q:如何得到機器上IIS中所有的WEB虛擬站點.
A:
var
InstallPath: String;
WebSite, WebServer, WebRoot: Variant;
count: Integer;
Flag: Boolean;
begin
Flag :=True;
count :=1;
while Flag do
begin
try
WebSite :=CreateOLEObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', inttostr(count));
Memo1.Lines.Add(WebServer.ServerComment);
inc(count);
except
flag :=False;
end;
end;
2. Q:通過程式控制預設WEB站點的啟動,停止,暫停
A:
//停止WEB站點
procedure TForm1.btn_stopClick(Sender: TObject);
var
InstallPath: String;
WebSite, WebServer, WebRoot: Variant;
begin
try
WebSite :=CreateOLEObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', '1');
if string(webserver.ServerState) ='2' then
begin
webserver.stop;
showmessage('停止服務成功');
end;
except
showmessage('停止服務失敗');
end;
end;
//啟動WEB站點
procedure TForm1.btn_startClick(Sender: TObject);
var
InstallPath: String;
WebSite, WebServer, WebRoot: Variant;
begin
try
WebSite :=CreateOLEObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', '1');
if string(webserver.ServerState) ='4' then
begin
webserver.start;
showmessage('啟動服務成功');
end;
except
showmessage('啟動服務失敗');
end;
end;
//暫停WEB站點
procedure TForm1.btn_pauseClick(Sender: TObject);
var
InstallPath: String;
WebSite, WebServer, WebRoot: Variant;
begin
try
WebSite :=CreateOLEObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', '1');
if string(webserver.ServerState) ='2' then
begin
webserver.pause;
showmessage('暫停服務成功');
end;
except
showmessage('暫停服務失敗');
end;
end;
3.虛擬目錄的創建
a:以程式在網上或超級猛料中也有,但都沒有指定應用程式名,以下程式已完成此功能
var
InstallPath: String;
WebSite, WebServer, WebRoot, VDir: Variant;
begin
WebSite := CreateOleObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', '1');
webserver.setinfo;
WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
VDir := WebRoot.Create('IIsWebVirtualDir', 'VariantTest');
VDir.AccessRead := True;
VDir.Path := 'C:\test';
VDir.AppCreate(True); //如果沒有這句的話,虛擬目錄就要人為的手工創建應用程式名
VDir.SetInfo;
Q:如何得到WEB站點的IP和埠
A:此次以WEB站點為例
var
InstallPath: String;
WebSite, WebServer, WebRoot: Variant;
ar: array of variant;
begin
try
WebSite :=CreateOLEObject('IISNamespace');
WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
WebServer := WebSite.GetObject('IIsWebServer', '3');
ar := webserver.ServerBindings; //把IIS虛擬站點綁定的IP、埠、功能變數名稱的值放入數組
showmessage(ar[0]);
except
end;
更多的請大家查看MSDN中有關ADSI的頁面