11using System . Runtime . ExceptionServices ;
22using System . Runtime . InteropServices ;
33using Microsoft . Management . Deployment ;
4+ using Microsoft . Win32 ;
45using UniGetUI . Core . Classes ;
56using UniGetUI . Core . Logging ;
67using UniGetUI . Core . Tools ;
@@ -24,6 +25,9 @@ internal sealed class NativeWinGetHelper : IWinGetManagerHelper
2425 private readonly Func < WinGet , IWinGetManagerHelper > _systemCliHelperFactory ;
2526 private readonly Func < IReadOnlyList < CatalogPackage > > ? _localPackagesProvider ;
2627 private readonly IPingetPackageDetailsProvider _pingetPackageDetailsProvider ;
28+ private const string UserUninstallKeyPath =
29+ @"Software\Microsoft\Windows\CurrentVersion\Uninstall" ;
30+ private const string InstalledIndexInvalidationValueName = "UniGetUI.InstalledIndexInvalidation" ;
2731 private int _activeLocalPackageQueries ;
2832 private ExceptionDispatchInfo ? _lastActivationException ;
2933
@@ -590,6 +594,7 @@ private IReadOnlyList<CatalogPackage> GetLocalWinGetPackages()
590594 try
591595 {
592596 logger . Log ( "OtherTask: GetWinGetLocalPackages" ) ;
597+ InvalidateInstalledIndexCache ( logger ) ;
593598 var availableCatalogs = GetReachableLocalCatalogReferences ( logger ) ;
594599 PackageCatalogReference installedSearchCatalogRef = CreateLocalCompositeCatalog (
595600 availableCatalogs ,
@@ -638,6 +643,30 @@ private IReadOnlyList<CatalogPackage> GetLocalWinGetPackages()
638643 }
639644 }
640645
646+ private static void InvalidateInstalledIndexCache ( INativeTaskLogger logger )
647+ {
648+ try
649+ {
650+ using RegistryKey ? uninstallKey = Registry . CurrentUser . OpenSubKey (
651+ UserUninstallKeyPath ,
652+ writable : true
653+ ) ;
654+ if ( uninstallKey is null )
655+ {
656+ logger . Log ( "No per-user uninstall key found; skipping WinGet installed index invalidation" ) ;
657+ return ;
658+ }
659+
660+ uninstallKey . SetValue ( InstalledIndexInvalidationValueName , Environment . ProcessId ) ;
661+ uninstallKey . DeleteValue ( InstalledIndexInvalidationValueName , throwOnMissingValue : false ) ;
662+ }
663+ catch ( Exception ex )
664+ {
665+ logger . Error ( $ "Could not invalidate the WinGet installed index: { ex . Message } ") ;
666+ Logger . Warn ( $ "Could not invalidate the WinGet installed index: { ex . Message } ") ;
667+ }
668+ }
669+
641670 private IReadOnlyList < PackageCatalogReference > GetReachableLocalCatalogReferences (
642671 INativeTaskLogger logger
643672 )
0 commit comments