在 Inno Setup iss 打包過程 中檢測 .NET 6 / .net 5 / .NET Core 運行環境是否存在或已安裝


為了將 .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
    • .NET Runtime 6.0
    • ASP.NET Core Runtime 5.0
    • ASP.NET Core Runtime 6.0
    • .NET Desktop Runtime 5.0
    • .NET Desktop Runtime 6.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM