Skip to content

Commit eeed2b9

Browse files
authored
Avalonia: Fix UnauthorizedAccessException with webview (#4692)
1 parent c358e41 commit eeed2b9

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/UniGetUI.Avalonia/App.axaml.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.IO;
23
using Avalonia;
34
using Avalonia.Controls.ApplicationLifetimes;
45
using Avalonia.Markup.Xaml;
@@ -10,6 +11,7 @@
1011
using UniGetUI.Avalonia.Views;
1112
using UniGetUI.Avalonia.Views.DialogPages;
1213
using UniGetUI.Core.Data;
14+
using UniGetUI.Core.Logging;
1315
using UniGetUI.PackageEngine;
1416
using CoreSettings = global::UniGetUI.Core.SettingsEngine.Settings;
1517

@@ -38,6 +40,12 @@ public override void OnFrameworkInitializationCompleted()
3840
{
3941
if (OperatingSystem.IsWindows())
4042
{
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+
4149
// Safety net for NativeWebView (WebView2) initialization failures thrown
4250
// asynchronously on the dispatcher. Without this the app crashes; with it
4351
// the Help page shows a fallback "Open in browser" button.
@@ -165,4 +173,21 @@ public static void ApplyTheme(string value)
165173
};
166174
}
167175

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+
168193
}

0 commit comments

Comments
 (0)