How could I move WebView2 folders to a different location?

3.5k views Asked by At

Is there any possible way of moving the runtimes and *.exe.WebView2 folders created by the Microsoft WebView2 WPF package to a different folder or perhaps embedding them? The 2 folders are selected in the screenshot below.

The 2 folders I dont want

I already use Costura.Fody to embed dlls. The ideal result would be if those 2 folders would be moved to the bin folder.

2

There are 2 answers

0
David Risney On BEST ANSWER

The .\{ExecutableName}.WebView2 folder is the default location of the user data folder. This contains all state generated by the WebView2 (cookies, HTTP cache, indexeddb storage, and so on) and by default is placed in the same folder as the host app's executable. But you can (and should) specify a different path to store the user's WebView2 state. For more information about the user data folder and where to place it see the Manage user data folders doc.

The second folder .\runtimes contains the WebView2Loader.dll file in different CPU architectures. There's currently no way to specify the path to find this but that has been requested (GH issue) and is in our backlog.

0
David On

You have to fix both folder issues separately

  1. The *.exe.WebView2 is a cache folder created when you run the webview2 inside the application. The following is how to redirect the folder to where you want it it to go, I used Path.GetTempPath().
CoreWebView2Environment cwv2Environment = await CoreWebView2Environment.CreateAsync(null, Path.GetTempPath(), new CoreWebView2EnvironmentOptions());
await webBrowser.EnsureCoreWebView2Async(cwv2Environment); 

Path.GetTempPath() will redirect to the users local app data temp so it would prevent permission issues on the servers and be easily maintained by infrastructure on growing hard drive space.

  1. A current temp solution is move the runtimes folder to the home directory if that is where you want it and the second line deletes it. In Visual Studio go to the project properties -> Compile -> Build Events and in the Post-build event command line and added the following.
xcopy /y $(TargetDir)runtimes\win-x64\native\WebView2Loader.dll $(TargetDir)$(OutDir)
RD /S /Q "$(TargetDir)runtimes\"