如何检测 net core 3.1 windows 桌面应用运行时是否通过 NSIS 脚本安装

问题描述

我有一个使用 NSIS(Nullsoft Scriptable Install System)创建的 .net Windows 应用程序安装设置。所有相关的 .net 框架和其他依赖项都通过 NSIS 脚本检查和安装。此设置最初是为 .net framework 4.8 应用程序构建的。但现在我想升级它以安装 .net core 3.1 应用程序。有人可以帮助我了解如何检测目标机器是否通过 NSIS 脚本安装了 .net core 3.1 windows 桌面运行时吗?

解决方法

检测 .NET 总是一种乐趣。一旦您知道一般方法,在 NSIS 中检测它应该相当简单,但即使找到该信息也很难。

我没有安装 .NET Core,所以我无法验证任何这些,但从我收集的信息来看,您应该在注册表中查找 the global install location

!include LogicLib.nsh
SetRegView 32 ; Always use the 32-bit view
ReadRegStr $1 HKLM "SOFTWARE\dotnet\Setup\InstalledVersions\x86" "InstallLocation" ; x86,x64,arm or arm64 and only for Core v3 and later
SetRegView lastused
${If} $1 == ""
    StrCpy $1 "$ProgramFiles32\dotnet" ; might want to change this if not x86?
${EndIf}

一旦您知道位置(或希望存在默认位置),您就应该使用命令行开关执行 dotnet.exe。根据您询问的对象,这可能是 --info--version--list-runtimes

nsExec::ExecToStack '"$1\dotnet.exe" --info'
Pop $0 ; Return
Pop $2 ; Output
${If} $0 != "error"
    MessageBox mb_ok "TODO: Parse $2"
${EndIf}