自助推荐
立即入驻

Inno Setup自动安装.net框架的脚本

博客2周前更新 桔子雨
61 00
Inno Setup自动安装.net框架的脚本

Inno Setup 是 Jordan Russell 和 Martijn Laan 为 Windows 程序提供的免费安装程序。Inno Setup 于 1997 年首次推出,如今在功能集和稳定性方面可与许多商业安装程序相媲美甚至超过许多商业安装程序。主要他是一个免费开源程序,根据协议Inno Setup可以完全免费商用。

Inno Setup自动安装.net框架的脚本简要教程

点击文件-新建然后根据向导创建安装包。自动生成安装脚本后,将下面[code]字段全部复制到安装脚本即可。代码里是判断4.7.2的版本,检测到已经安装不会重复来安装,可节省安装时间,如要安装不同版本的.net框架替换下面代码中的.net包的版本号和文件名。

[Code]
function IsDotNetDetected(version: string; service: cardinal): boolean;
var
    key, versionKey: string;
    install, release, serviceCount, versionRelease: cardinal;
    success: boolean;
begin
    versionKey := version;
    versionRelease := 0;

    if version = 'v1.1' then begin
        versionKey := 'v1.1.4322';
    end else if version = 'v2.0' then begin
        versionKey := 'v2.0.50727';
    end else if Pos('v4.', version) = 1 then begin
        versionKey := 'v4\Full';
        case version of
        'v4.5':   versionRelease := 378389;
        'v4.5.1': versionRelease := 378675;
        'v4.5.2': versionRelease := 379893;
        'v4.6':   versionRelease := 393295;
        'v4.6.1': versionRelease := 394254;
        'v4.6.2': versionRelease := 394802;
        'v4.7':   versionRelease := 460798;
        'v4.7.1': versionRelease := 461308;
        'v4.7.2': versionRelease := 461808;
        'v4.8':   versionRelease := 528040;
        end;
    end;

    key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

    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;

    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;

    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
    Path: string;
    ResultCode: Integer;
begin
    Result := True;

    // 先检查是否已安装 .NET Framework 4.7.2
    if not IsDotNetDetected('v4.7.2', 0) then 
    begin
        // 如果未安装 .NET Framework 4.7.2,则进行解压和安装
        MsgBox('软件运行需要 Microsoft .NET Framework 4.7.2, 即将开始安装.NET Framework。', mbInformation, MB_OK);

        // 解压 .NET Framework 安装程序
        ExtractTemporaryFile('dotnet472.exe');//框架在安装包里的路径
        Path := ExpandConstant('{tmp}\dotnet472.exe');//解压到系统临时路径

        // 执行 .NET Framework 安装程序
        Exec(Path, '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);

        // 再次检查是否成功安装
        if not IsDotNetDetected('v4.7.2', 0) then
        begin
            if MsgBox('.NET Framework 4.7.2 组件安装失败,无法运行程序,是否继续安装?', mbError, MB_YESNO) = IDNO then
            begin
                Result := False;
                Exit;
            end;
        end;
    end;

    // 删除临时文件(如果解压了)
    if FileExists(ExpandConstant('{tmp}\dotnet472.exe')) then
    begin
        DeleteFile(ExpandConstant('{tmp}\dotnet472.exe'));
    end;

    Result := True;
end;
© 版权声明

相关文章