WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在 Inno Setup 中检测 .NET 5 / .NET Core 运行环境是否存在

Posted on 2024-05-27 21:47  WebEnh  阅读(34)  评论(0编辑  收藏  举报

为了将 .NET 5 / .NET Core 应用程序部署到客户机,我们可以编写 Inno Setup 代码来判断客户机是否安装了必要的运行环境。.NET 官方仓库 中提供了一个名为 NetCoreCheck 的项目,可以用于检测指定的 .NET 5 / .NET Core 环境是否存在。编译好的文件可以从以下两个地址下载:

文件名 下载地址
netcorecheck.exe https://go.microsoft.com/fwlink/?linkid=2135256
netcorecheck_x64.exe https://go.microsoft.com/fwlink/?linkid=2135504

需要注意的是,以上两个文件需要依赖 Visual C++ 2015 Redistributable ,如果不具备该环境,则运行会报错。

仅需要将运行环境名称和版本号传递给以上程序即可通过程序的返回值来判断指定的环境是否存在,如果返回值为 0 ,则代表客户机已经安装了指定的运行环境。

@echo off
netcorecheck.exe Microsoft.WindowsDesktop.App 5.0.0
if %ERRORLEVEL% EQU 0 (
echo 已安装
) else (
echo 未安装
)

Inno Setup 脚本编写

在 iss 文件中,我们需要将 netcorecheck.exe 和 netcorecheck_x64.exe 加入到 Files 节点:

[Files]
 
// dotnet core 运行环境检测依赖文件,不需要复制到输出文件。
// download netcorecheck.exe: https://go.microsoft.com/fwlink/?linkid=2135256
// download netcorecheck_x64.exe: https://go.microsoft.com/fwlink/?linkid=2135504
Source: "netcorecheck.exe"; Flags: dontcopy noencryption
Source: "netcorecheck_x64.exe"; Flags: dontcopy noencryption

定义 IsNetCoreInstalled 方法来检测 .net core 环境是否已经安装:

// architecture helper functions
function IsX64: Boolean;
begin
Result := Is64BitInstallMode;
end;
 
function GetString(const x86, x64: String): String;
begin
if IsX64 then begin
Result := x64;
end else begin
Result := x86;
end;
end;
 
function GetArchitectureSuffix: String;
begin
Result := GetString('', '_x64');
end;
// 检测 .net core 环境是否已经安装
// https://github.com/dotnet/deployment-tools/tree/master/src/clickonce/native/projects/NetCoreCheck
function IsNetCoreInstalled(const Version: String): Boolean;
var
ResultCode: Integer;
begin
if not FileExists(ExpandConstant('{tmp}{\}') + 'netcorecheck' + GetArchitectureSuffix + '.exe') then begin
ExtractTemporaryFile('netcorecheck' + GetArchitectureSuffix + '.exe');
end;
Result := ShellExec('', ExpandConstant('{tmp}{\}') + 'netcorecheck' + GetArchitectureSuffix + '.exe', Version, '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0);
end;

使用方法

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

if IsNetCoreInstalled('Microsoft.NETCore.App 5.0.0') then
begin
Log('Microsoft.NETCore.App 5.0.0 is installed');
end
else
begin
Log('Microsoft.NETCore.App 5.0.0 is not installed');
end;

下面的表格中列出了更多的参数信息:

名称 检验参数
.NET Core 3.1 运行环境 Microsoft.NETCore.App 3.1.0
ASP.NET Core 3.1 运行环境 Microsoft.AspNetCore.App 3.1.0
.NET Core 3.1 桌面运行环境 Microsoft.WindowsDesktop.App 3.1.0
.NET 5 运行环境 Microsoft.NETCore.App 5.0.0
ASP.NET Core 5 运行环境 Microsoft.AspNetCore.App 5.0.0
.NET 5 桌面运行环境 Microsoft.WindowsDesktop.App 5.0.0

引用来源

本文是笔者根据 GitHub 仓库 InnoDependencyInstaller 中的内容整理修改而来。

Inno Setup Dependency Installer 可以在你的应用程序安装过程中下载并安装任何依赖关系,如.NET, Visual C++或SQL Server Express Redistributable! 此外,还可以轻松地添加自己的依赖关系。

  • .NET
    • .NET Framework 1.1
    • .NET Framework 1.1 Service Pack 1
    • .NET Framework 2.0 + Service Pack 2
    • .NET Framework 3.5 + Service Pack 1
    • .NET Framework 4.0 Client
    • .NET Framework 4.0 Full
    • .NET Framework 4.5.2
    • .NET Framework 4.6.2
    • .NET Framework 4.7.2
    • .NET Framework 4.8
    • .NET Core Runtime 3.1
    • ASP.NET Core Runtime 3.1
    • .NET Desktop Runtime 3.1
    • .NET Runtime 5.0
    • ASP.NET Core Runtime 5.0
    • .NET Desktop Runtime 5.0
  • C++
    • Visual C++ 2005 Redistributable
    • Visual C++ 2008 Redistributable
    • Visual C++ 2010 Redistributable
    • Visual C++ 2012 Redistributable
    • Visual C++ 2013 Redistributable
    • Visual C++ 2015-2019 Redistributable
  • SQL
    • SQL Server 2008 Express R2 + Service Pack 2
    • SQL Server 2012 Express + Service Pack 4
    • SQL Server 2014 Express + Service Pack 3
    • SQL Server 2016 Express + Service Pack 2
    • SQL Server 2017 Express
    • SQL Server 2019 Express
  • DirectX End-User Runtime
  • Windows Installer 4.5

在此,感谢仓库作者 DomGries 的辛勤劳动和付出。

// contribute: https://github.com/DomGries/InnoDependencyInstaller
// official article: https://codeproject.com/Articles/20868/Inno-Setup-Dependency-Installer