Search code examples
c#skype

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


I have a C# program that will call skype for a given phone number or Skype ID.

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

The code does work, for both UWP Skype or desktop Skype. But I want to direct a message to user if Skype (neither windows store version or desktop version) is not install.

I can detect desktop version by looking at registry:

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.
            }
        }
    }
}

The above way can find if skype.exe is installed.

What I want to know is: how can I correctly detect if UWP version of Skype installed?


Solution

  • I found a not-so-clean answer, using powershell commands, from this question and this msdn article.

    I need powershell 2.0 installed, and add a reference to System.Management.Automation.dll from windows sdk.

    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...
            }
        }
    }