Delphi创建IIS虚拟目录的方法
Windows IIS
服务安装后配置很麻烦,想像一下你的客户不懂这些配置,你又烦于这些流水线式的配置,如果写一个工具实现一键部署显得那么高档,逼格高的一批。
以下实现Delphi创建IIS虚拟目录的方法。
具体代码
unit MainUnt;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, FileCtrl, Buttons,Activeds_TLB;typeTIISConfigFrm = class(TForm)edtAlias: TEdit;Label1: TLabel;dlbIIS: TDirectoryListBox;dcbIIS: TDriveComboBox;Label2: TLabel;edtPath: TEdit;GroupBox1: TGroupBox;cbRead: TCheckBox;cbScript: TCheckBox;cbExecute: TCheckBox;cbWrite: TCheckBox;cbBrowse: TCheckBox;bbtOK: TBitBtn;lblPath: TLabel;procedure dlbIISChange(Sender: TObject);procedure bbtOKClick(Sender: TObject);procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }end;function ADsGetObject(const PathName: WideString; const GUID:TGUID; out I: IUnknown): HRESULT; stdcall;varIISConfigFrm: TIISConfigFrm;implementation{$R *.dfm}function ADsGetObject;external 'ActiveDS.dll' name 'ADsGetObject';procedure TIISConfigFrm.dlbIISChange(Sender: TObject);beginedtPath.Text:=dlbIIS.Directory;end;procedure TIISConfigFrm.bbtOKClick(Sender: TObject);varI: IADsContainer;ADs: IADs;beginif Length(Trim(edtAlias.Text))=0 then beginApplication.MessageBox('别名不可以为空!','警告');Exit;end;if Length(Trim(edtPath.Text))=0 then beginApplication.MessageBox('请选定虚拟目录位置!','警告');Exit;end;if ADsGetObject('IIS://localhost', IID_IADsContainer, IUnknown(I)) = S_Ok then
begin //IIS已经安装if ADsGetObject('IIS://localhost/w3svc', IID_IADsContainer, IUnknown(I)) = S_Ok then begin //Web服务器存在ADs := IADs(I.GetObject('IIsWebServer', '1')); //取得服务if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then begin //服务支持ADs := IADs(I.GetObject('IIsWebVirtualDir', 'Root')); //在Web服务器的Root下建立虚拟目录if ADs.QueryInterface(IID_IADsContainer, I) = S_OK then begin //服务支持tryADs := IADs(I.Create('IIsWebVirtualDir', edtAlias.Text)); //建立虚拟目录,别名为edtAlias.TextexceptApplication.MessageBox('这个别名已经存在,请选择另外的别名!','警告');Exit;end; //try exceptADs.Put('AccessRead', cbRead.Checked); //设定各参数ADs.Put('AccessWrite', cbWrite.Checked);ADs.put('AccessScript',cbScript.Checked);ADs.Put('AccessExecute',cbExecute.Checked);ADs.put('EnableDirBrowsing',cbBrowse.Checked);ADs.Put('Path', edtPath.text);ADs.Put('DefaultDoc','Default.asp, Default.html, Default.htm, ndex.asp, Index.html, Index.htm, Home.asp, Home.Html, Home.htm');ADs.Put('EnableDefaultDoc',True);//允许打开默认文件ADs.SetInfo; //保存参数Application.MessageBox('您的设定已经保存。','恭喜');end;end;end;end elseApplication.MessageBox('您的计算机上没有安装IIS或者您无权访问IIS。','警告');end;procedure TIISConfigFrm.FormCreate(Sender: TObject);beginedtPath.Text:=dlbIIS.Directory;end;end.