Showing WPF window having webView2 control gives error dialog with description - Microsoft Edge can't read and write to its data directory

Ajay Gera 45 Reputation points
2025-02-07T15:57:54.6933333+00:00

Showing WPF window having webView2 control gives error dialog with description - Microsoft Edge can't read and write to its data directory

I have Webview2 control in WPF Window as shown below in webViewStrings.xaml file

<Window x:Class="abc.webViewStrings"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

    xmlns:local="clr-namespace:abc"

    xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"

    mc:Ignorable="d"

    Width="1100" 

    Height="800">

<DockPanel>

    <wv2:WebView2 Name="webView" />

</DockPanel>
```</Window>

On invoking webViewStrings as modal dialog i.e. by calling webViewStrings.ShowDialog();  

I try to load static test.html and some *.js files into weview2 control in VS2022 Enterprise edition.

On loading/launching webview2, I am getting error dialog with following description 

description: Microsoft Edge can't read and write to its data directory:

C:\program files\microsoft visual studio\2022\Community\common7\ide\devenv.exe.webview2\EBWebView

[errorDilaog.jpg](/api/attachments/1a3ee0df-335d-4b09-a2b6-dd1298972216?platform=QnA)

I don't see any error on Visual Studio Professional edition.

How to fix above error in VS2022 Community? 

What is the purpose of webview2 cache, when it gets created and how it is used by Webview2?

Does html and JavaScript loaded into Webview2 control gets cached?

Is there way to avoid this caching by webview2?

When this cache would be cleared?

I attached the pic of webview2 cache. it has many folders.

[webViewCache.jpg](/api/attachments/c1403c8b-76db-4ccd-8614-4735afca2bc6?platform=QnA)

Below is the code snippet of webViewStrings.xaml.cs

internal async Task<bool> InitializeWebViewAsync()

{

  AttachControlEventHandlers(webView);  

  await webView.EnsureCoreWebView2Async().ConfigureAwait(true);

  webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;

  return true.

}

void AttachControlEventHandlers(WebView2 control)

{

  control.NavigationStarting += WebView_NavigationStarting;

  control.NavigationCompleted += WebView_NavigationCompleted;

  control.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;

}

void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)

{

  if (e.IsSuccess)

  { }

}

void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)

{

  //_isNavigating = true;

  RequeryCommands();

  CoreWebView2NavigationKind kind = e.NavigationKind;  

}

void RequeryCommands()

{

  CommandManager.InvalidateRequerySuggested();

}

void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)

{

  if (e.IsSuccess)

  {

```powershell
webView.CoreWebView2.SetVirtualHostNameToFolderMapping(appassets.example, <filesPath>, CoreWebView2HostResourceAccessKind.Allow);

webView.Source = new Uri(abc.html);    
```  }

  

  //handlers to get receives messages from JavaScript 

  //com1.Instance.addMessageHandlers("tes1", this.test1);

}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,828 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 4,370 Reputation points Microsoft Vendor
    2025-02-10T07:43:46.6066667+00:00

    Hi, @Ajay Gera. Welcome to Microsoft Q&A. 

    I ran the program you provided in Visual Studio 2022 Enterprise and Visual Studio 2022 Community, and both ran successfully.

    Test.Html

    Picture1

    Button Binding JavaScript:

    function sendMessage() {
        window.chrome.webview.postMessage("Hello from JavaScript!");
    }
    

    Picture2

    This seems to be related to your environment. You could consider testing it on other computers to see if the error message also appears, so as to rule out problems in the program.

    In the current environment, you need to troubleshoot permissions issues. General direction: 1.Run the program as an administrator 2. adjust the permissions of the target file 3. reset the Edge browser to default settings (reinstall the permissions of the Edge browser) 4. Use a shared folder to store cache files, etc.

    Purpose of cached data: This data could help web pages load and run faster.

    When created: The UDF is created on startup of the WebView2 host app, if the UDF doesn't exist. Related Documents

    What kind of data is stored in the UDF: Related Documents

    How to delete and when to delete cached data:Related Documents 1Related Documents 2


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.