如何检测在 C# 中安装了 UWP 应用程序 (Skype)?

How can I detect an UWP application (Skype) is installed in C#?

我有一个 C# 程序可以为给定的 phone 号码或 Skype ID 调用 Skype。

string input;
//...
string uriSkype = $"Skype:{input}?call";
Process p = Process.Start(uriSkype);
if (p != null)
{
    p.WaitForExit();
    p.Close();
}

该代码适用于 UWP Skype 或桌面版 Skype。 但是,如果未安装 Skype(windows 商店版本或桌面版本),我想向用户发送消息。

我可以通过查看注册表来检测桌面版本:

RegistryKey SoftwareKey = Registry.CurrentUser.OpenSubKey("Software");
if (SoftwareKey != null)
{
    RegistryKey SkypeKey = SoftwareKey.OpenSubKey("Skype");
    if (SkypeKey != null)
    {
        RegistryKey PhoneKey = SkypeKey.OpenSubKey("Phone");
        if (PhoneKey != null)
        {
            object objSkypePath = PhoneKey.GetValue("SkypePath");
            if (objSkypePath != null)
            {
                // here I know the path of skype.exe is installed.
            }
        }
    }
}

以上方法可以查到是否安装了skype.exe

我想知道的是:如何正确检测是否安装了 UWP 版本的 Skype?

我使用 powershell 命令从 and this msdn article.

中找到了一个不太干净的答案

我需要安装 powershell 2.0,并从 windows sdk 添加对 System.Management.Automation.dll 的引用。

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    // get the installed apps list
    PowerShellInstance.AddScript("Get-AppxPackage | ft Name, PackageFullName -AutoSize");
    // format output to a string
    PowerShellInstance.AddCommand("Out-String");
    // invoke execution on the pipeline (collecting output)
    Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
    // loop through each output object item
    foreach (PSObject outputItem in PSOutput)
    {
        string strOut = outputItem.ToString();
        if (strOut.Contains("Microsoft.SkypeApp"))
        {
            // do stuff...
        }
    }
}