Check if Optional Package Exists

3

I've created a UWP app and a UWP optional package. I got that all set up using this documentation: https://docs.microsoft.com/en-us/windows/uwp/packaging/optional-packages-with-executable-code

Everything is working as expected and I can use code in the optional package. But obviously if I remove the optional package, my code won't work. I don't want to have my app crash if the "optional" package isn't installed.

What is the accepted way of checking if an optional package is installed? Do I just catch the exception? That seems a bit hacky.


Example: When I reference Class1 (in my optional package) it throws this exception:

System.Runtime.InteropServices.COMException: 'Class not registered (Exception from HRESULT: 0x80040154)'

enter image description here

c#
uwp
asked on Stack Overflow Jun 27, 2018 by James Esh

1 Answer

1

What is the accepted way of checking if an optional package is installed?

You could use PackageCatalog class to get all the packages installed.

List<string> optionalPackageList = new List<string>(); 
foreach (var package in Windows.ApplicationModel.Package.Current.Dependencies) 
{ 
    if (package.IsOptional) 
    { 
        optionalPackageList.Add(package.Id.FamilyName); 
    } 
} 

You could check if the optionalPackageList has contain the specified option package . For more please refer this document.

answered on Stack Overflow Jun 28, 2018 by Nico Zhu - MSFT • edited Jun 28, 2018 by Nico Zhu - MSFT

User contributions licensed under CC BY-SA 3.0