Inno Setup 检测已安装的.NET Framework 版本支持检测.NET Framework 4.5 4.6 4.7 4.8

Inno Setup 检测已安装的.NET Framework 版本

;.NET Framework 安装文件
#define DotNetSetupFile "ndp48-web.exe"
; 上述消息放置到脚本的顶部

[Code]
const CDotNetSetupFile = '{#DotNetSetupFile}';
var VNeedRestart: boolean;
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. 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
        // from url https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
          '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
          'v4.7':   versionRelease := 460798; // 460805 On all other Windows operating systems (including other Windows 10 operating systems)
          'v4.7.1': versionRelease := 461308; // 461310 On all other Windows operating systems (including other Windows 10 operating systems)
          'v4.7.2': versionRelease := 461808; // 461814 On all Windows operating systems other than Windows 10 April 2018 Update and Windows Server, version 1803
          'v4.8'  : versionRelease := 528040; // On Windows 10 May 2019 Update and Windows 10 November 2019 Update: 528040; On Windows 10 May 2020 Update and Windows 10 October 2020 Update: 528372; On all other Windows operating systems (including other Windows 10 operating systems): 528049
        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;

function InitializeSetup(): Boolean;
var IEPath, NetV2DownUrl, DotNetPath:string;
var ResultCode:Integer;
begin
    VNeedRestart := false;

    if not IsDotNetDetected('v4.8', 0) then begin
      
      if MsgBox('系统缺少程序运行组件.Net Framework 4.8,是否立刻下载并安装?', mbConfirmation, MB_YESNO) = idYes then begin
          // 临时解压安装文件
          ExtractTemporaryFile(CDotNetSetupFile);
          DotNetPath := ExpandConstant('{tmp}\'+CDotNetSetupFile);
          // 运行
          if Exec(DotNetPath,'/norestart /passive /showfinalerror','',SW_SHOWNORMAL,ewWaitUntilTerminated,ResultCode) then begin
              if ResultCode = 0 then begin
                  result := true;
              end else if (ResultCode = 1641) or (ResultCode = 3010) then begin
                  // 需要重新启动才能完成安装。 此消息指示安装成功。
                  VNeedRestart := true;
                  result := true;
              end else begin
                  result := false;
              end
          end else begin
              MsgBox('运行.Net Framework 4.8安装程序失败!',mbError,MB_OK);
              result := false;
          end
      end
    end else begin
        result := true;
    end
end;

function NeedRestart(): Boolean;
begin
  result := VNeedRestart;
end;

安装程序可以从微软官方下载:
https://dotnet.microsoft.com/download/dotnet-framework

参考:

确定已安装的 .NET Framework 版本 | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed

Inno Setup 判断.NET是否安装_tcmtang的博客-CSDN博客 https://blog.csdn.net/tcmtang/article/details/49932205

Inno Setup 检测已安装的.NET Framework 版本 - 知乎
https://zhuanlan.zhihu.com/p/51658788

你可能感兴趣的:(输入法开发,安装包制作,inno,setup,pascal,.net,installer,c#)