You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WiX Toolset 5中准确检测已安装的最高.NET版本?

检测已安装的最高.NET版本(WiX Toolset 5)

方法1:使用WiX官方DotNet扩展(推荐)

WiX Toolset 5提供了专门的WixToolset.DotNet.wixext扩展,能准确检测已安装的.NET Core/.NET 5+版本,自动处理卸载残留问题。

  1. 先在项目中引用该扩展:
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"
     xmlns:dotnet="http://wixtoolset.org/schemas/v4/wxs/dotnet">
  1. 使用DotNetCoreSearch元素搜索指定运行时的最高版本:
<Property Id="NETCORE_VERSION">
  <dotnet:DotNetCoreSearch RuntimeType="desktop" MinimumVersion="6.0.0" />
</Property>
  • RuntimeType可选值:desktop(桌面运行时)、aspnetcore(ASP.NET Core运行时)、shared(共享宿主)
  • 该元素会自动返回符合条件的最高已安装版本,不会读取卸载残留的无效注册表项。
  1. 基于检测结果添加安装条件:
<Condition Message="需要安装.NET 6.0或更高版本。">
  <![CDATA[Installed OR NETCORE_VERSION]]>
</Condition>

方法2:读取准确的注册表路径(无扩展依赖)

如果不想依赖扩展,可以读取SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.NETCore.App路径下的子项——这里的每个子项对应一个实际安装的.NET版本,卸载后对应的子项会被删除,不会残留。

  1. 使用RegistrySearch遍历子项,提取版本号:
<Property Id="NETCORE_HIGHEST_VERSION">
  <RegistrySearch Id="NetCoreVersions"
                  Root="HKLM"
                  Key="SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.NETCore.App"
                  Type="raw"
                  Win64="yes" />
</Property>
  1. 配合自定义动作解析注册表返回的子项列表,筛选出最高版本(可通过C#或VBScript自定义动作实现版本比较逻辑)。

方法3:通过dotnet命令行检测

直接调用系统中的dotnet.exe命令获取已安装运行时列表,解析输出得到最高版本:

  1. 创建自定义动作执行命令:
<CustomAction Id="GetDotNetVersion"
              BinaryKey="CustomActions"
              DllEntry="GetHighestDotNetVersion"
              Execute="immediate"
              Return="check" />
  1. 自定义动作的C#代码示例:
[CustomAction]
public static ActionResult GetHighestDotNetVersion(Session session)
{
    var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "dotnet.exe",
            Arguments = "--list-runtimes",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };

    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    var versions = new List<Version>();
    foreach (var line in output.Split('\n'))
    {
        if (line.StartsWith("Microsoft.NETCore.App"))
        {
            var versionStr = line.Split(' ')[1];
            if (Version.TryParse(versionStr, out var version))
            {
                versions.Add(version);
            }
        }
    }

    if (versions.Any())
    {
        session["NETCORE_HIGHEST_VERSION"] = versions.Max().ToString();
    }

    return ActionResult.Success;
}
  1. 在安装序列中调用该自定义动作,并添加条件判断。

内容的提问来源于stack exchange,提问作者TrinaE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 04:57:15