如何使用PowerShell从C#获取存储应用版本?

问题描述 投票:1回答:1

我不知道为什么结果集合为空?如何获取PowerShell命令的确切输出?

using System;
using System.Management.Automation;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            PowerShell ps = PowerShell.Create();
            var results = ps.AddScript("(Get-AppxPackage \"Adobe.CC.XD\" | Select Version).Version").Invoke();

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }
    }
}
c# powershell .net-core
1个回答
0
投票

我找到了一种不使用PowerShell对象的解决方法。

static string GetStoreAppVersion(string appName)
{
    var process = new Process
    {
        StartInfo =
        {
            FileName = "powershell.exe",
            Arguments = $"-Command (Get-AppxPackage \"{appName}\" | Select Version).Version",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    process.Start();
    process.WaitForExit();

    var output = process.StandardOutput.ReadToEnd();
    var version = output.Replace(System.Environment.NewLine, string.Empty);

    if (string.IsNullOrWhiteSpace(version))
    {
        return null;
    }

    return version;
}
© www.soinside.com 2019 - 2024. All rights reserved.