本文主要描述的是如何利用Delphi对Web
IIS进行控制,应该说常用的功能基本都有了。为了共用我已把它做成了一个独立的单元文件,大家可直接下载,这样我们只要直接在程序 uses
uIIS就可以使用了,同时我也做了一个演示程序。
一、如何得到机
器上IIS中所有的WEB虚拟站点.
- function GetWebSiteList(slWebSiteList:TStrings):Integer;
- var
- InstallPath: String;
- WebSite, WebServer, WebRoot: Variant;
- iCount: Integer;
- Flag: Boolean;
- begin
- Result := 0;
- Flag :=True;
- iCount :=1;
-
- slWebSiteList.Clear;
- while Flag do
- begin
- try
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
-
- WebServer := WebSite.GetObject('IIsWebServer', InttoStr(iCount));
- slWebSiteList.Add(WebServer.ServerComment);
- Result := iCount;
- Inc(iCount);
- except
- Flag :=False;
- end;
- end;
- end;
二、如何得到IIS的安装目录(如
C:\Inetpub\wwwroot)呢,
- function GetWebSitePath(const iWebSiteIndex:integer=1):string;
- var
- WebSite, WebServer, WebRoot: Variant;
- begin
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
- Result := WebRoot.Path;
- end;
三、如何得到WEB站点的IP和端
口
- function GetWebSiteIP(const iWebSiteIndex:Integer=1):string;
- 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', IntToStr(iWebSiteIndex));
- ar := WebServer.ServerBindings;
- Result := ar[0];
- except
- end;
- end;
四、如何判断IIS下是否存在某个虚拟目录
- function VirtualDirIsExists(const strVirtualDir:string;
- const iWebSiteIndex:Integer=1):Boolean;
- var
- WebSite, WebServer, WebRoot, VDir: Variant;
- Flag: Boolean;
- begin
- Result := False;
- Flag :=True;
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
- try
- vdir := WebRoot.GetObject('IIsWebVirtualDir', strVirtualDir);
- Result := True;
- except
-
- Result := False;
- end;
- end;
五、如何创建 IIS 虚拟目录:
- function CreateVirtualDir(const strVirtualDir,strDir,AppName:string;
- const iWebSiteIndex:Integer=1):Boolean;
- var
- WebSite, WebServer, WebRoot, VDir: Variant;
- begin
- try
- WebSite := CreateOleObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- webserver.SetInfo;
- WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
- VDir := WebRoot.Create('IIsWebVirtualDir', strVirtualDir);
-
- VDir.AccessRead := True;
- VDir.AccessWrite := False;
- VDir.AccessExecute := False;
- VDir.AccessScript := True;
- VDir.EnableDirBrowsing := False;
- VDir.Path := strDir;
- VDir.AppCreate(True);
- VDir.AppFriendlyName :=AppName;
- VDir.SetInfo;
- Result := True;
- except
- Result := False;
- end;
- end;
-
-
-
-
-
-
-
-
-
-
-
-
六、如何删除虚拟目录
- function DeleteVirtualDir(const strVirtualDir: string;const iWebSiteIndex:Integer=1): Boolean;
- var
- WebSite, WebServer, WebRoot,vdir: Variant;
- begin
- Result := True;
- try
- WebSite := CreateOleObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
- WebRoot.Delete('IIsWebVirtualDir',strVirtualDir);
- except
- Result := False;
- end;
- end;
七、如何删除虚拟目录应用
程序名
- function DeleteVirtualDirApp(const strVirtualDir: string;const iWebSiteIndex:Integer=1): Boolean;
- var
- WebSite, WebServer, WebRoot,vdir: Variant;
- begin
- Result := True;
- try
- WebSite := CreateOleObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
- vdir :=WebRoot.GetObject('IIsWebVirtualDir', strVirtualDir);
- vdir.AppDelete;
- vdir.SetInfo;
- except
- Result := False;
- end;
- end;
八、如何通过程序控制默认
WEB站点的例子,三种状态2是运行,4是停止,6是暂停
-
- function StopWebServer(const iWebSiteIndex:Integer=1):Boolean;
- var
- WebSite, WebServer: Variant;
- begin
- try
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- if string(WebServer.ServerState) ='2' then
- begin
- WebServer.Stop;
-
- Result := True;
- end;
- except
-
- Result := False;
- end;
- end;
-
-
- function StartWebServer(const iWebSiteIndex:Integer=1):Boolean;
- var
- WebSite, WebServer: Variant;
- begin
- try
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- if string(webserver.ServerState) ='4' then
- begin
- WebServer.Start;
-
- Result := True;
- end;
- except
-
- Result := False;
- end;
- end;
-
-
- function PauseWebServer(const iWebSiteIndex:Integer=1):Boolean;
- var
- WebSite, WebServer: Variant;
- begin
- try
- WebSite :=CreateOLEObject('IISNamespace');
- WebSite := WebSite.GetObject('IIsWebService', 'localhost/w3svc');
- WebServer := WebSite.GetObject('IIsWebServer', IntToStr(iWebSiteIndex));
- if string(WebServer.ServerState) ='2' then
- begin
- WebServer.Pause;
-
- Result := True;
- end;
- except
-
- Result := False;
- end;
- end;
九、Q:
如何重启IIS A:用IISRESET /? 命令
- function ControlIIS(const strAction:string='restart'):Boolean;
-
-
-
-
-
- var
- sCmd:string;
- begin
- sCmd:='IISReset/'+strAction;
- try
- WinExec(pchar(sCmd),SW_HIDE);
-
- Result := True;
- except
-
- Result := False;
- end;
- end;
我已把它们做成了一个单元文件,大家可直接下载,这样你只要在程序 uses
uIIS;就可以使用了。
而且我也做了一个演示程序:
演示程序下载:
阅读(414) | 评论(0) | 转发(0) |