|
1 | 1 | using System.Diagnostics; |
| 2 | +using System.IO; |
2 | 3 | using Avalonia; |
3 | 4 | using Avalonia.Controls.ApplicationLifetimes; |
4 | 5 | using Avalonia.Markup.Xaml; |
|
10 | 11 | using UniGetUI.Avalonia.Views; |
11 | 12 | using UniGetUI.Avalonia.Views.DialogPages; |
12 | 13 | using UniGetUI.Core.Data; |
| 14 | +using UniGetUI.Core.Logging; |
13 | 15 | using UniGetUI.PackageEngine; |
14 | 16 | using CoreSettings = global::UniGetUI.Core.SettingsEngine.Settings; |
15 | 17 |
|
@@ -38,6 +40,12 @@ public override void OnFrameworkInitializationCompleted() |
38 | 40 | { |
39 | 41 | if (OperatingSystem.IsWindows()) |
40 | 42 | { |
| 43 | + // Redirect WebView2 user-data folder to a writable temp location. |
| 44 | + // Without this, WebView2 tries to write next to the executable in |
| 45 | + // C:\Program Files\, which is read-only for non-admin users and |
| 46 | + // causes UnauthorizedAccessException (E_ACCESSDENIED) on startup. |
| 47 | + SetUpWebViewUserDataFolder(); |
| 48 | + |
41 | 49 | // Safety net for NativeWebView (WebView2) initialization failures thrown |
42 | 50 | // asynchronously on the dispatcher. Without this the app crashes; with it |
43 | 51 | // the Help page shows a fallback "Open in browser" button. |
@@ -165,4 +173,21 @@ public static void ApplyTheme(string value) |
165 | 173 | }; |
166 | 174 | } |
167 | 175 |
|
| 176 | + private static void SetUpWebViewUserDataFolder() |
| 177 | + { |
| 178 | + try |
| 179 | + { |
| 180 | + string webViewPath = Path.Join(Path.GetTempPath(), "UniGetUI", "WebView"); |
| 181 | + if (!Directory.Exists(webViewPath)) |
| 182 | + Directory.CreateDirectory(webViewPath); |
| 183 | + |
| 184 | + Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", webViewPath); |
| 185 | + } |
| 186 | + catch (Exception e) |
| 187 | + { |
| 188 | + Logger.Warn("Could not set up data folder for WebView2"); |
| 189 | + Logger.Warn(e); |
| 190 | + } |
| 191 | + } |
| 192 | + |
168 | 193 | } |
0 commit comments