InnoSetup可在在腳本中插入[Code]代碼段,其中的代碼可以通過事件驅動,支持的主要事件如下:
function InitializeSetup(): Boolean; ——安裝程序初始化,返回值決定安裝程序是否繼續執行。
function NextButtonClick(CurPageID: Integer): Boolean; ——點擊下一步按鈕,返回值決定安裝程序是否繼續執行。
function BackButtonClick(CurPageID: Integer): Boolean; ——點擊上一步按鈕,返回值決定安裝程序是否繼續執行。
function InitializeUninstall(): Boolean; ——卸載程序初始化,返回值決定卸載程序是否繼續執行。
...
從這些事件我們可以看到InitializeSetup()滿足我們的要求,我們可以在這個時候去檢查注冊表或者是系統文件來判斷客戶機器上是否安裝了.Net Framework,從而進行自動安裝或者下載安裝的操作。
[Code] function InitializeSetup: Boolean; var Path,tmppath:string ; ResultCode: Integer; dotNetV2RegPath:string; dotNetV2DownUrl:string; dotNetV2PackFile:string; begin dotNetV2RegPath:='SOFTWARE\Microsoft\.NETFramework\Policy\v4.0'; dotNetV2DownUrl:='http://dl1sw.baidu.com/soft/9b/15910/Microsoft.NET.exe?version=585709662'; dotNetV2PackFile:='{src}\dotNetFx40_Full_x86_x64.exe'; //先在注冊表查找.net4.0是否存在 if RegKeyExists(HKLM, dotNetV2RegPath) then begin Result := true; end //如果注冊表里面沒有發現.net4.0 else begin if MsgBox('系統檢測到您沒有安裝.Net Framework4.0運行環境,是否立即安裝?', mbConfirmation, MB_YESNO) = idYes then begin //和setup同級目錄下的donet安裝包 Path := ExpandConstant(dotNetV2PackFile); //先抽取到臨時目錄 tmppath := ExpandConstant('{tmp}\dotNetFx40_Full_x86_x64.exe'); ExtractTemporaryFile('dotNetFx40_Full_x86_x64.exe'); msgbox(tmppath, mbConfirmation, MB_YESNO); Exec(tmppath, '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode); if(FileOrDirExists(tmppath)) then begin Exec(tmppath, '/q', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode); if RegKeyExists(HKLM, dotNetV2RegPath) then begin Result := true; end else begin MsgBox('未能成功安裝.Net Framework4.0運行環境,系統將無法運行,本安裝程序即將退出!',mbInformation,MB_OK); end end else begin if MsgBox('軟件安裝目錄中沒有包含.Net Framework4.0的安裝程序,是否立即下載后安裝?', mbConfirmation, MB_YESNO) = idYes then begin Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe'); Exec(Path, dotNetV2DownUrl , '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode); MsgBox('請安裝好.Net Framework4.0環境后,再運行本安裝包程序!',mbInformation,MB_OK); Result := false; end else begin MsgBox('不下載安裝.Net Framework4.0運行環境,系統將無法運行,本安裝程序即將退出!',mbInformation,MB_OK); Result := false; end end end else begin MsgBox('沒有安裝.Net Framework2.0運行環境,系統將無法運行,本安裝程序即將退出!',mbInformation,MB_OK); Result := false; end; end; end;
參考鏈接1:http://blog.csdn.net/hualei/article/details/2628312
參考鏈接2:http://zhoufoxcn.blog.51cto.com/792419/279243/