Inno Setup 检测系统是否安装.Net Framework

Inno Setup用Delphi写成,其官方网站同时也提供源程序免费下载。它虽不能与Installshield这类恐龙级的安装制作软件相比,但也当之无愧算是后起之秀。Inno Setup是一个免费的安装制作软件,小巧、简便、精美是其最大特点,支持pascal脚本。

最近公司开发了一个WinForm的项目,部署人员在制作安装程序的时候问到怎么在安装程序中自动检测是否安装.Net Framework,于是使用InnoSetup制作的安装程序,InnoSetup本身是支持检测系统是否安装.Net Framework。.

Inno Setup 判断.NET是否安装,如未安装则提示下载安装,且不继续执行安装步骤:

[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//    'v4.7'          .NET Framework 4.7//    'v4.7.1'        .NET Framework 4.7.1//    'v4.7.2'        .NET Framework 4.7.2//    'v4.8'          .NET Framework 4.8//// 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. requiredvar    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 before Win10 November Update          'v4.6.2': versionRelease := 394802; // 394806 before Win10 Anniversary Update          'v4.7':   versionRelease := 460798; // 460805 before Win10 Creators Update          'v4.7.1': versionRelease := 461308; // 461310 before Win10 Fall Creators Update          'v4.7.2': versionRelease := 461808; // 461814 before Win10 April 2018 Update          'v4.8':   versionRelease := 528040; // 528049 before Win10 May 2019 Update        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;
procedure InitializeWizard();begin  WizardForm.LICENSEACCEPTEDRADIO.Checked:=true;end;
function GetHKLM: Integer;begin  if IsWin64 then    Result := HKLM64  else    Result := HKLM32;end;

以下代码展示了检测 .NET 运行时的代码:

   //     begin   // if not IsDotNetDetected('v4.6', 0) then begin   //     MsgBox('MyApp requires Microsoft .NET Framework 4.6.'#13#13    //        'Please use Windows Update to install this version,'#13   //         'and then re-run the MyApp setup program.', mbInformation, MB_OK);   //     result := false; //   end else  //    MsgBox('1111111111111MyApp requires Microsoft .NET Framework 4.6.'#13#13     //       'Please use Windows Update to install this version,'#13     //       'and then re-run the MyApp setup program.', mbInformation, MB_OK);    //    result := true;        //end;