|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System.IO; |
| 5 | +using Microsoft.Win32; |
5 | 6 |
|
6 | 7 | namespace Files.App.Services.PreviewPopupProviders |
7 | 8 | { |
@@ -48,28 +49,59 @@ private async Task DoPreviewAsync(string path) |
48 | 49 | public async Task<bool> DetectAvailability() |
49 | 50 | { |
50 | 51 | var exeName = "PowerToys.Peek.UI.exe"; |
51 | | - var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
52 | | - var perUserPath = Path.Combine(localAppData, "PowerToys", "WinUI3Apps", exeName); |
53 | | - |
54 | | - // User path |
55 | | - if (File.Exists(perUserPath)) |
| 52 | + if (FindPeekPathFromRegistry(exeName) is { } registryPath && File.Exists(registryPath)) |
56 | 53 | { |
57 | | - _peekExecutablePath = perUserPath; |
| 54 | + _peekExecutablePath = registryPath; |
58 | 55 | return true; |
59 | 56 | } |
60 | 57 |
|
61 | | - // Machine-wide path |
62 | | - string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); |
63 | | - string machinePath = Path.Combine(programFiles, "PowerToys", "WinUI3Apps", exeName); |
| 58 | + // Not found |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + private static string? FindPeekPathFromRegistry(string exeName) |
| 63 | + { |
| 64 | + string[] uninstallRegistryPaths = |
| 65 | + [ |
| 66 | + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", |
| 67 | + @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", |
| 68 | + ]; |
64 | 69 |
|
65 | | - if (File.Exists(machinePath)) |
| 70 | + foreach (var hive in new[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine }) |
66 | 71 | { |
67 | | - _peekExecutablePath = machinePath; |
68 | | - return true; |
| 72 | + foreach (var uninstallPath in uninstallRegistryPaths) |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + using var baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default); |
| 77 | + using var uninstallKey = baseKey.OpenSubKey(uninstallPath); |
| 78 | + if (uninstallKey is null) |
| 79 | + continue; |
| 80 | + |
| 81 | + foreach (var subKeyName in uninstallKey.GetSubKeyNames()) |
| 82 | + { |
| 83 | + using var appKey = uninstallKey.OpenSubKey(subKeyName); |
| 84 | + if (appKey is null) |
| 85 | + continue; |
| 86 | + |
| 87 | + var displayName = appKey.GetValue("DisplayName") as string; |
| 88 | + if (string.IsNullOrWhiteSpace(displayName) |
| 89 | + || displayName.IndexOf("PowerToys", StringComparison.OrdinalIgnoreCase) < 0) |
| 90 | + continue; |
| 91 | + |
| 92 | + var installLocation = appKey.GetValue("InstallLocation") as string; |
| 93 | + if (!string.IsNullOrWhiteSpace(installLocation)) |
| 94 | + return Path.Combine(installLocation, "WinUI3Apps", exeName); |
| 95 | + } |
| 96 | + } |
| 97 | + catch |
| 98 | + { |
| 99 | + // Ignore registry access issues and continue fallback resolution. |
| 100 | + } |
| 101 | + } |
69 | 102 | } |
70 | 103 |
|
71 | | - // Not found |
72 | | - return false; |
| 104 | + return null; |
73 | 105 | } |
74 | 106 | } |
75 | 107 | } |
0 commit comments