DotNetSpartan
DotNetSpartan

Reputation: 1001

Set Cache directory for WebView2

I am using WebView2 in WPF control to host the new edge.

In my code, I want to cache the cookie and browser specific data to a cache directory. The cache location should be set in the CoreWebView2EnvironmentOptions when creating the CoreWebView2Environment.

Is there any way we can achieve this ? Thanks in advance.

Upvotes: 13

Views: 23425

Answers (3)

wayofthefuture
wayofthefuture

Reputation: 9445

This works too:

Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", "C:\MyDirectory\")

Upvotes: 12

Morten Brudvik
Morten Brudvik

Reputation: 479

I have made a WPF application using WebView2 that sets the cache directory to user appdata folder. Below is a snippet of the code from my application.

var webView2Environment = await CoreWebView2Environment.CreateAsync(null, _cacheFolderPath);
await kioskBrowser.EnsureCoreWebView2Async(webView2Environment);

kioskBrowser.Source = new Uri(url);

You can find the rest of the code here KioskBrowser (GitHub)

Upvotes: 18

David Risney
David Risney

Reputation: 4377

You can set the user data folder in the CoreWebView2Environment.CreateAsync method. Its the second parameter and the other parameters may be null.

You can then have the WPF WebView use your CoreWebView2Environment to create its CoreWebView2 by calling the WebView2.EnsureCoreWebView2Async method.

However, you must call EnsureCoreWebView2Async before you set the WebView2.Source property as setting that property implicitly causes initialization to begin with a default CoreWebView2Environment.

You can read more about WPF WebView2 control initialization in the WebView2 documentation summary section.

Upvotes: 16

Related Questions