0

I have written a small program, what downloads 2 PDFs (marketplace shipping label), depending on a given purchase order on commandline. The App itself is build as unpackaged WinUI3. After downloading the PDFs, the Main Window starts, and the PDFs are visible with WebView2 in 2 different Pages. I included the WindowsSDK in the Build-Output, and its built as framework dependend.

The curious thing now is the following. If i'm starting the App from projects bin directory from commandline, the App works as expected. It downloads the PDFs, the MainWindow launches, and if i'm clicking on the pages, WebView2 launches as Page component and shows the PDF.

I'm planning to deliver it with a Installer (not MSIX), so unpackaged. For testing i copied the content of my bin directory to a new directory under C:\Program Files. If i'm starting the App from that location, with the same arguments, the App starts, i can access the two pages, but the pages are empty. No WebView2 is loaded (or loaded and not visible).

Maybe anyone has seen that too, and knows, how to fix that?

1

1 Answer 1

1

When you don't explicitly set a user data folder, WebView2 creates one next to your EXE in a sibling folder named <YourExe>.WebView2. That works from bin\ during development, but once you copy the app under C:\Program Files\..., a standard user can't write there. Initialization then fails with an empty WebView2.

The fix is to create the WebView2 environment with a writable user data folder (e.g., under %LOCALAPPDATA%) and initialize your controls with that environment.

using Microsoft.Web.WebView2.Core;

var cachePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
    "MyApp", "WebView2Cache");
Directory.CreateDirectory(cachePath);

var env = await CoreWebView2Environment.CreateAsync(
    browserExecutableFolder: null,
    userDataFolder: cachePath,
    options: new CoreWebView2EnvironmentOptions());

await webView2.EnsureCoreWebView2Async(env);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.