網上有很多關于用DELPHI來建立IIS的虛擬目錄的例子,但都是需要加載類庫,我這里則采用寫注冊表的方式來實現,方法簡單,主要是不需要加入類的復雜過程,唯一的不同是需要重啟電腦后才能生效。代碼如下:
PRocedure TForm1.Button1Click(Sender: TObject);
var
RegODBC:TRegistry;
registerTemp : TRegistry;
SysPath: array [0..255] of char;
begin
RegODBC:=TRegistry.create; //訪問注冊表
RegODBC.RootKey:=HKEY_LOCAL_MACHINE;
RegODBC.OpenKey('/SYSTEM/ControlSet001/Services/W3SVC/Parameters/Virtual Roots',True);
getsystemdirectory(SysPath,255);
if regodbc.ValueExists('/fire') then
begin
suimessage1.Text:='本機WEB已存在名為fire的虛擬目錄。'+#13#10+'請將此fire刪除或重命名。';
suimessage1.IconType:=suistop;
suimessage1.ButtonCount:=1;
suimessage1.Caption:='錯誤';
if suimessage1.ShowModal=mrok then
winexec(pchar(SysPath+'/inetsrv/inetmgr.exe'),sw_shownormal);
exit;
end
else
begin
registerTemp := TRegistry.Create; //建立一個Registry實例
with registerTemp do
begin
RootKey:=HKEY_LOCAL_MACHINE;//設置根鍵值為HKEY_LOCAL_MACHINE
//找到或創建/SYSTEM/ControlSet001/Services/W3SVC/Parameters/Virtual Roots,寫入IIS配置信息
if OpenKey('/SYSTEM/ControlSet001/Services/W3SVC/Parameters/Virtual Roots',True) then
begin
WriteString('/fire','E:/fire,,205');
end
else//創建鍵值失敗
begin
suimessage1.Text:='IIS配置失敗,本程序即將關閉。'+#13#10+'關閉后請先檢查Internet服務管理器,排除錯誤或安裝后再運行本程序。';
suimessage1.IconType:=suistop;
suimessage1.ButtonCount:=1;
suimessage1.Caption:='錯誤';
if suimessage1.ShowModal=mrok then
application.Terminate ;
end;
CloseKey;
Free;
end;
end;
RegODBC.Free;
end;
說明:代碼中用到了suipack4控件的suimessagedialog組件,例子的確認窗口也可通過Application.MessageBox()來替代。如大家對此有興趣的話可以與我聯系。[email protected] QQ:49055028
新聞熱點
疑難解答
圖片精選