inno setup 检查当前 .NET Framework 环境并安装
大约分为两种方式
- 安装过程中连接官网下载地址 下载到本地 进行安装(看网络带宽 微软官网国内有时不灵)
- 将脱机安装文件和安装包打包在一起 安装时直接读取本地文件进行安装(安装包文件会很大 毕竟.net环境也在里面)
参考网址
1. 微软官网:.NET Framework 部署指南(针对开发人员)
2. 官网inno setup 打包教程: Installing .NET Framework 4.5 automatically with Inno Setup
3. 特别实在的一篇教程: inno setup安装制作软件详细使用步骤(含检测net版本环境)
4. 解决打包文件夹问题:InnoSetup 打包文件夹的解决方案
主要是Code 部分怎么写
Code
个人总结拼接出来的code 检查版本的方法就是去注册表去找
[Code]
// 'Software\Microsoft\NET Framework Setup\NDP\v4\Full' 当前安装的.net 环境地址
// 461814 当前 .net 版本 对照 https://docs.microsoft.com/zh-cn/dotnet/framework/deployment/deployment-guide-for-developers
// 判断是否有安装.net 4.7.2 运行环境
function Framework472IsNotInstalled(): Boolean;
var
bSuccess: Boolean;
regVersion: Cardinal;
begin
Result := True;
// 检查版本
bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', regVersion);
if (True = bSuccess) and (regVersion >= 461814) then begin
Result := False;
end;
end;
// 下面两个方法 组合调用 安装.net 环境
procedure InstallFramework;
var
StatusText: string;
ResultCode: Integer;
begin
StatusText := WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption := '正在安装 .NET Framework 4.7.2. 可能需要几分钟的时间...';
WizardForm.ProgressGauge.Style := npbstMarquee;
try
//打包在安装包里面的 ndp472.exe .net 环境安装程序
if not Exec(ExpandConstant('{tmp}\ndp472.exe'), '/passive /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
MsgBox('.NET 安装失败,请联系管理员,代码: ' + IntToStr(ResultCode) + '.', mbError, MB_OK);
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
DeleteFile(ExpandConstant('{tmp}\ndp472.exe'));
end;
end;
// 该过程提供用户完成预安装和安装之后的任务,更多的是提供了安装过程中的状态。 相当于钩子函数
//if CurStep=ssinstall then
// MsgBox('准备写入文件了', mbInformation, MB_OK);
// if CurStep=ssPostinstall then
// MsgBox('文件复制完成', mbInformation, MB_OK);
// if CurStep=ssDone then
// MsgBox('软件成功安装', mbInformation, MB_OK);
procedure CurStepChanged(CurStep: TSetupStep);
begin
case CurStep of
ssPostInstall:
begin
if Framework472IsNotInstalled() then
begin
InstallFramework();
end;
end;
end;
end;
[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"; LicenseFile: "compiler:License\chinesesimp.txt"
Name: "chinesetra"; MessagesFile: "compiler:Languages\ChineseTraditional.isl"; LicenseFile: "compiler:License\chinesetra.txt"
Name: "english"; MessagesFile: "compiler:Languages\English.isl";LicenseFile: "compiler:License\english.txt"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "..所在路径..\Debug\ndp472.exe"; DestDir: "{app}"; Flags: ignoreversion
微软官网演示
[Code]
function Framework47IsNotInstalled(): Boolean;
var
bSuccess: Boolean;
regVersion: Cardinal;
begin
Result := True;
bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', regVersion);
if (True = bSuccess) and (regVersion >= 378389) then begin
Result := False;
end;
end;
procedure InitializeWizard;
begin
if Framework47IsNotInstalled() then
begin
idpAddFile('https://dotnet.microsoft.com/download/dotnet-framework/net472', ExpandConstant('{tmp}\NetFrameworkInstaller.exe'));
idpDownloadAfter(wpReady);
end;
end;
procedure InstallFramework;
var
StatusText: string;
ResultCode: Integer;
begin
StatusText := WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption := 'Installing .NET Framework 4.7.2. This might take a few minutes...';
WizardForm.ProgressGauge.Style := npbstMarquee;
try
if not Exec(ExpandConstant('{tmp}\NetFrameworkInstaller.exe'), '/passive /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
MsgBox('.NET installation failed with code: ' + IntToStr(ResultCode) + '.', mbError, MB_OK);
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
DeleteFile(ExpandConstant('{tmp}\NetFrameworkInstaller.exe'));
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
case CurStep of
ssPostInstall:
begin
if Framework47IsNotInstalled() then
begin
InstallFramework();
end;
end;
end;
end;