Inno Setup检测安装.Net Framework运行环境脚本



1.本地安装.Net Framework

[Files]
Source: "G:\myapp\dotnetfx.exe"; Flags: dontcopy noencryption

[Code]
function InitializeSetup(): Boolean;
var ResultCode:Integer;
begin      
    if not IsDotNetDetected('v2.0', 0) then begin
      ExtractTemporaryFile('dotnetfx.exe');
      Exec(ExpandConstant('{tmp}\dotnetfx.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
      //经测试.NET 2.0安装成功Exec仍返回false,改用IsDotNetDetected再检测一次
      if not IsDotNetDetected('v2.0', 0) then begin
        MsgBox('.Net Framework2.0组件安装失败,无法运行程序,安装程序即将退出!',mbInformation,MB_OK);
        result := false;
      end else begin
        result := true;
      end
    end else begin
      result := true;
    end
end;


2.在线下载安装.Net Framework

[Code]
function InitializeSetup(): Boolean;
var IEPath, NetV2DownUrl:string;
var ResultCode:Integer;
begin      
    if not IsDotNetDetected('v2.0', 0) then begin
      if MsgBox('系统缺少程序运行组件.Net Framework 2.0,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then begin
        if IsWin64 then begin
          NetV2DownUrl := 'https://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x64.exe';
        end else begin
          NetV2DownUrl := 'https://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x86.exe';
        end;
        IEPath := ExpandConstant('{pf}\Internet Explorer\iexplore.exe');
        Exec(IEPath, NetV2DownUrl, '' , 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 else begin
        result := true;
    end
end;


附:Inno Setup 检测已安装的.NET Framework 版本(复制到Code节点)

function IsDotNetDetected(version: string; service: cardinal): boolean;
// Indicates whether the specified version and service pack of the .NET Framework is installed.
//
// version -- Specify one of these strings for the required .NET Framework version:
//    'v1.1'          .NET Framework 1.1
//    'v2.0'          .NET Framework 2.0
//    'v3.0'          .NET Framework 3.0
//    'v3.5'          .NET Framework 3.5
//    'v4\Client'     .NET Framework 4.0 Client Profile
//    'v4\Full'       .NET Framework 4.0 Full Installation
//    'v4.5'          .NET Framework 4.5
//    'v4.5.1'        .NET Framework 4.5.1
//    'v4.5.2'        .NET Framework 4.5.2
//    'v4.6'          .NET Framework 4.6
//    'v4.6.1'        .NET Framework 4.6.1
//    'v4.6.2'        .NET Framework 4.6.2
//
// service -- Specify any non-negative integer for the required service pack level:
//    0               No service packs required
//    1, 2, etc.      Service pack 1, 2, etc. required
var
    key, versionKey: string;
    install, release, serviceCount, versionRelease: cardinal;
    success: boolean;
begin
    versionKey := version;
    versionRelease := 0;

    // .NET 1.1 and 2.0 embed release number in version key
    if version = 'v1.1' then begin
        versionKey := 'v1.1.4322';
    end else if version = 'v2.0' then begin
        versionKey := 'v2.0.50727';
    end

    // .NET 4.5 and newer install as update to .NET 4.0 Full
    else if Pos('v4.', version) = 1 then begin
        versionKey := 'v4\Full';
        case version of
          'v4.5':   versionRelease := 378389;
          'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
          'v4.5.2': versionRelease := 379893;
          'v4.6':   versionRelease := 393295; // 393297 on Windows 8.1 and older
          'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
          'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
        end;
    end;

    // installation key group for all .NET versions
    key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

    // .NET 3.0 uses value InstallSuccess in subkey Setup
    if Pos('v3.0', version) = 1 then begin
        success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
    end else begin
        success := RegQueryDWordValue(HKLM, key, 'Install', install);
    end;

    // .NET 4.0 and newer use value Servicing instead of SP
    if Pos('v4', version) = 1 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
    end else begin
        success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
    end;

    // .NET 4.5 and newer use additional value Release
    if versionRelease > 0 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
        success := success and (release >= versionRelease);
    end;

    result := success and (install = 1) and (serviceCount >= service);
end;

相关链接

[1].用Inno Setup来解决.NetFramework安装问题:http://zhoufoxcn.blog.51cto.com/792419/279243/
[2].使用Inno Setup 打包.NET程序,并自动安装.Net Framework:http://www.cnblogs.com/xiaogangqq123/archive/2012/03/19/2405730.html

评论: 0 | 引用: 0 | 查看次数: 749
发表评论
登录后再发表评论!