检查是否使用CustomAction和Wix安装了.NETCore [英] Check if .NETCore is installed using CustomAction with Wix

查看:57
本文介绍了检查是否使用CustomAction和Wix安装了.NETCore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果未安装NetCore 3.1(预览版),我想取消安装

I want to cancel the installation if the NetCore 3.1 (preview) is not installed

我创建了这个CustomAction:

I create this CustomAction :

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;

namespace WixCustomAction
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CheckDotNetCore31Installed(Session session)
        {
            session.Log("Begin CheckDotNetCore31Installed");

            RegistryKey lKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost");

            var version = (string)lKey.GetValue("Version");

            session["DOTNETCORE31"] = version == "3.1.0-preview3.19553.2" ? "1" : "0";

            return ActionResult.Success;
        }
    }
}

然后在WXS文件中:

<<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">

   <Product ...>

  (...)

    <Property Id="DOTNETCORE31">0</Property>

    <Condition Message="You must first install the .NET Core 3.1 Runtime">
      Installed OR DOTNETCORE31="1"
    </Condition>

    <InstallExecuteSequence>
      <Custom Action="Check.NetCore" Before="LaunchConditions">NOT Installed</Custom>
    </InstallExecuteSequence>

  </Product>

  <Fragment>
    <Binary Id="WixCA.dll" SourceFile="$(var.WixCustomAction.TargetDir)$(var.WixCustomAction.TargetName).CA.dll" />
    <CustomAction Id="Check.NetCore" BinaryKey="WixCA.dll" DllEntry="CheckDotNetCore31Installed" Execute="immediate"  />
  </Fragment>

这是我遇到的问题,因为我总是收到警告消息.一个主意 ?谢谢

And this is where I have a problem because I always get the warning message. An idea ? thanks

推荐答案

也许最好使用cmd来检查.net核心版本, dotnet --version 命令,以避免Windows体系结构依赖性

maybe it will be better to check .net core version using cmd with dotnet --version command, to avoid windows architecture dependencies

这种情况下的自定义操作示例:

custom action sample for this case:

    [CustomAction]
    public static ActionResult CheckVersion(Session session)
    {
       var minVersion = new Version(3, 1, 1);
        var command = "/c dotnet --version";// /c is important here
        var output = string.Empty;
        using (var p = new Process())
        {
            p.StartInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = command,
                UseShellExecute = false,                    
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
            };
            p.Start();
            while (!p.StandardOutput.EndOfStream)
            {
                output += $"{p.StandardOutput.ReadLine()}{Environment.NewLine}";
            }
            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                session["DOTNETCORE1"] = "0";
                return ActionResult.Success;
                //throw new Exception($"{p.ExitCode}:{ p.StandardError.ReadToEnd()}");
            }

            //you can implement here your own comparing algorithm
            //mine will not work with -perview string, but in this case you can just 
            //replace all alphabetical symbols with . using regex, for example
            var currentVersion = Version.Parse(output);
            session["DOTNETCORE1"] = (currentVersion < minVersion) ? "0" : "1";
            
            return ActionResult.Success;
         }
      

更新:正如亚当(Adam)所提到的那样,我对该代码段是错误的-它仅适用于SDK.这是获得运行时的另一个方法:

UPDATE: As Adam mentioned, I was wrong with that snippet - it works only for SDKs. Here's another one to get runtimes:

    static readonly List<string> runtimes = new List<string>()
    {
        "Microsoft.NETCore.App",//.NET Runtime
        "Microsoft.AspNetCore.App",//ASP.NET Core Runtime
        "Microsoft.WindowsDesktop.App",//.NET Desktop Runtime
    };

    [CustomAction]
    public static ActionResult CheckVersion(Session session)
    {
        var minVersion = new Version(3, 1, 1);
        var command = "/c dotnet --list-runtimes";// /c is important here
        var output = string.Empty;
        using (var p = new Process())
        {
            p.StartInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = command,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
            };
            p.Start();
            while (!p.StandardOutput.EndOfStream)
            {
                output += $"{p.StandardOutput.ReadLine()}{Environment.NewLine}";
            }
            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                session["DOTNETCORE1"] = "0";
                return ActionResult.Success;
                //throw new Exception($"{p.ExitCode}:{ p.StandardError.ReadToEnd()}");
            }
            session["DOTNETCORE1"] = (GetLatestVersionOfRuntime(runtimes[0], output) < minVersion) ? "0" : "1";
            return ActionResult.Success;
        }
    }

    private static Version GetLatestVersionOfRuntime(string runtime, string runtimesList)
    {
        var latestLine = runtimesList.Split(Environment.NewLine).ToList().Where(x => x.Contains(runtime)).OrderBy(x => x).LastOrDefault();
        if (latestLine != null)
        {
            Regex pattern = new Regex(@"\d+(\.\d+)+");
            Match m = pattern.Match(latestLine);
            string versionValue = m.Value;
            if (Version.TryParse(versionValue, out var version))
            {
                return version;
            }
        }
        return null;
    }

这篇关于检查是否使用CustomAction和Wix安装了.NETCore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆