我不知道为什么结果集合为空?如何获取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();
}
}
}
我找到了一种不使用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;
}