Inno Setup检测Miscrosoft .NET Framework版本一键安装

检测Miscrosoft .NET Framework并打包多个exe程序实现一键安装

背景

  1. 当你需要安装多个应用,而又不想一个一个点击的时候,如何来实现打包多个exe程序并实现一键安装;
  2. 当你的程序依赖Miscrosoft .NET Framework版本时,如何检测是否安装或版本安装是否符合预期?
  3. 当不符合预期的时候,如何实现自动安装Miscrosoft .NET Framework版本?

实现步骤

工具

Inno Setup:
https://jrsoftware.org/isinfo.php

Miscrosoft .NET Framework:
https://www.microsoft.com/zh-cn/download/details.aspx?id=17718

打包

  1. 启动Inno Setup, 如下图

Inno Setup检测Miscrosoft .NET Framework版本一键安装_第1张图片
Inno Setup检测Miscrosoft .NET Framework版本一键安装_第2张图片

  1. 傻瓜式一步一步往下走即可,最后会生成一个iss文件,编译完成即可生成打包的程序

Inno Setup检测Miscrosoft .NET Framework版本一键安装_第3张图片
Inno Setup检测Miscrosoft .NET Framework版本一键安装_第4张图片

  1. 参考iss文件的帮助文档,可对iss打包内容进行扩展
  • ; 注释
  • [Setup]区段 安装配置
  • [Files]区段 文件安装
  • [run]区段是可选的,用来指定在程序完成安装后,在安装程序显示最终对话框前要执行的一些程序; 打包多个exe时,可按顺序在此处一次实现静默安装
  • 静默安装参考:https://www.cnblogs.com/pengsn/p/12986956.html

Miscrosoft .NET Framework 检测

  1. [Files] 可以将依赖文件打入exe中
[Files]
Source: "NDP452-KB2901907-x86-x64-AllOS-ENU.exe"; DestDir: "{app}" ; 
Source: "VideoClient1.0.exe"; DestDir: "{app}" ;  
  1. 检测代码如下
[Code]
 function IsDotNetDetected(version: string; service: cardinal): boolean;

 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. 安装Miscrosoft .NET Framework
 ExtractTemporaryFile('NDP452-KB2901907-x86-x64-AllOS-ENU.exe');
 Exec(ExpandConstant('{tmp}\NDP452-KB2901907-x86-x64-AllOS-ENU.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);

完整代码

; -- psn.iss --
; Demonstrates copying 2 files

; SEE THE DOCUMENTATION FOR DETAILS ON CREATING .ISS SCRIPT FILES!
;

[Setup]
AppName=VideoClient
AppVersion=1.0
WizardStyle=modern
DefaultDirName={autopf}\VideoClient
DefaultGroupName=VideoClient
UninstallDisplayIcon={app}\VideoClient1.0.exe
Compression=lzma2
SolidCompression=yes
AlwaysRestart=no
OutputDir=userdocs:Inno Setup Examples Output

[Files]
Source: "NDP452-KB2901907-x86-x64-AllOS-ENU.exe"; DestDir: "{app}" ; 
Source: "VideoClient1.0.exe"; DestDir: "{app}" ; 

[run]
Filename: "{app}\VideoClient1.0.exe";Parameters:"/sp- /silent /norestart"; Flags: hidewizard; 

[Icons]
Name: "{group}\VideoClient1.0.exe"; Filename: "{app}\VideoClient1.0.exe"

[Code]
 function IsDotNetDetected(version: string; service: cardinal): boolean;

 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;
 
 
 function InitializeSetup(): Boolean;
 var Path:string;
 ResultCode: Integer;
 begin
     if not IsDotNetDetected('v4.5', 0) then begin
         ExtractTemporaryFile('NDP452-KB2901907-x86-x64-AllOS-ENU.exe');
         Exec(ExpandConstant('{tmp}\NDP452-KB2901907-x86-x64-AllOS-ENU.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
         result := true;
     end else
         result := true;
 end;

你可能感兴趣的:(Inno Setup检测Miscrosoft .NET Framework版本一键安装)