Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Optional Package Exists

Tags:

c#

uwp

I've created a UWP app and a UWP optional package. I got that all set up using this documentation: https://learn.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

like image 812
James Esh Avatar asked Sep 02 '25 15:09

James Esh


1 Answers

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.

like image 123
Nico Zhu - MSFT Avatar answered Sep 05 '25 05:09

Nico Zhu - MSFT