南安康美邮政编码:安装程序自动检测安装.Net Framework运行环境(使用InnoSetup) - 疾...

来源:百度文库 编辑:偶看新闻 时间:2024/03/28 17:38:42
安装程序自动检测安装.Net Framework运行环境(使用InnoSetup) 收藏
最近公司开发了一个WinForm的项目,部署人员在制作安装程序的时候问到怎么在安装程序中自动检测是否安装.Net Framework,由于是使用InnoSetup制作的安装程序,InnoSetup本身是支持Pascal脚本的,于是撰写了如下的代码来实现自动检测安装.Net Framework的功能。InnoSetup可在在脚本中插入[Code]代码段,其中的代码可以通过事件驱动,支持的主要事件如下:    function InitializeSetup(): Boolean; ——安装程序初始化,返回值决定安装程序是否继续执行。   
function NextButtonClick(CurPageID: Integer): Boolean; ——点击下一步按钮,返回值决定安装程序是否继续执行。    
function BackButtonClick(CurPageID: Integer): Boolean; ——点击上一步按钮,返回值决定安装程序是否继续执行。   
function InitializeUninstall(): Boolean; ——卸载程序初始化,返回值决定卸载程序是否继续执行。  
...
从这些事件我们可以看到InitializeSetup()满足我们的要求,我们可以在这个时候去检查注册表或者是系统文件来判断客户机器上是否安装了.Net Framework,从而进行自动安装或者下载安装的操作。最终代码如下:
--------------------------------------------------------------------------------
view plaincopy to clipboardprint?
[Code]  
 
function InitializeSetup: Boolean;  
 
var Path:string ;  
 
    ResultCode: Integer;  
 
    dotNetV2RegPath:string;  
 
    dotNetV2DownUrl:string;  
 
    dotNetV2PackFile:string;  
 
begin  
 
  dotNetV2RegPath:='SOFTWARE\Microsoft\.NETFramework\policy\v2.0';  
 
  dotNetV2DownUrl:='http://www.xxx.com/down/dotNetFx_v2.0(x86).exe';  
 
  dotNetV2PackFile:='{src}\dotNetFx_v2.0(x86).exe';  
 
  if RegKeyExists(HKLM, dotNetV2RegPath) then  
 
  begin  
 
    Result := true;  
 
  end  
 
  else 
 
  begin  
 
    if MsgBox('系统检测到您没有安装.Net Framework2.0运行环境,是否立即安装?', mbConfirmation, MB_YESNO) = idYes then  
 
    begin  
 
 
 
      Path := ExpandConstant(dotNetV2PackFile);  
 
      if(FileOrDirExists(Path)) then  
 
      begin  
 
        Exec(Path, '/q', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);  
 
        if RegKeyExists(HKLM, dotNetV2RegPath) then  
 
        begin  
 
           Result := true;  
 
        end  
 
        else 
 
        begin  
 
           MsgBox('未能成功安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);  
 
        end  
 
      end  
 
      else 
 
      begin  
 
        if MsgBox('软件安装目录中没有包含.Net Framework的安装程序,是否立即下载后安装?', mbConfirmation, MB_YESNO) = idYes then  
 
        begin  
 
          Path := ExpandConstant('{pf}\Internet Explorer\iexplore.exe');  
 
          Exec(Path, dotNetV2DownUrl , '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);  
 
          MsgBox('请安装好.Net Framework2.0环境后,再运行本安装包程序!',mbInformation,MB_OK);  
 
          Result := false;  
 
        end  
 
        else 
 
        begin  
 
          MsgBox('不下载安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);  
 
          Result := false;  
 
        end  
 
      end  
 
    end  
 
    else 
 
    begin  
 
      MsgBox('没有安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);  
 
      Result := false;  
 
    end;  
 
  end;  
 
end;  [Code]function InitializeSetup: Boolean;var Path:string ;    ResultCode: Integer;    dotNetV2RegPath:string;    dotNetV2DownUrl:string;    dotNetV2PackFile:string;begin  dotNetV2RegPath:='SOFTWARE\Microsoft\.NETFramework\policy\v2.0';  dotNetV2DownUrl:='http://www.xxx.com/down/dotNetFx_v2.0(x86).exe';  dotNetV2PackFile:='{src}\dotNetFx_v2.0(x86).exe';  if RegKeyExists(HKLM, dotNetV2RegPath) then  begin    Result := true;  end  else  begin    if MsgBox('系统检测到您没有安装.Net Framework2.0运行环境,是否立即安装?', mbConfirmation, MB_YESNO) = idYes then    begin       Path := ExpandConstant(dotNetV2PackFile);      if(FileOrDirExists(Path)) then      begin        Exec(Path, '/q', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);        if RegKeyExists(HKLM, dotNetV2RegPath) then        begin           Result := true;        end        else        begin           MsgBox('未能成功安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);        end      end      else      begin        if MsgBox('软件安装目录中没有包含.Net Framework的安装程序,是否立即下载后安装?', mbConfirmation, MB_YESNO) = idYes then        begin          Path := ExpandConstant('{pf}\Internet Explorer\iexplore.exe');          Exec(Path, dotNetV2DownUrl , '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);          MsgBox('请安装好.Net Framework2.0环境后,再运行本安装包程序!',mbInformation,MB_OK);          Result := false;        end        else        begin          MsgBox('不下载安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);          Result := false;        end      end    end    else    begin      MsgBox('没有安装.Net Framework2.0运行环境,系统将无法运行,本安装程序即将退出!',mbInformation,MB_OK);      Result := false;    end;  end;end;
使用的时候将上面的代码段复制到您的安装脚本中进行编译(您可以更改dotNetV2RegPath、dotNetV2PackFile和dotNetV2DownUrl来更改默认的参数),最后将.Net Framework的安装包“dotNetFx_v2.0(x86).exe”放在与安装程序同一目录中即可。安装程序执行时将检查客户计算机是否安装了.Net Framework 2.0,如果没有安装,则会调用同一目录下的.Net安装包进行安装,如同一目录下没有.Net安装包,则提示从网络上下载.Net安装包。.Net安装包安装完毕后将自动继续执行您的安装程序。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hualei/archive/2008/07/09/2628312.aspx