首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PowerShell从C#获取商店应用程序版本?

问如何使用PowerShell从C#获取商店应用程序版本?
EN

Stack Overflow用户
提问于 2020-01-28 23:54:21
回答 1查看 56关注 0票数 1

我不知道为什么结果集合是空的?

如何获得PowerShell命令的准确输出?

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-29 14:28:26

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

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59952511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档