Search code examples
c#winformswebview2

WebView2 how to load local file?


I have a WebView2 control in my WinForm .NET Framework 4.7.2, how can i load inside it my local html file?

I was trying to set the .Source with file:// extention but nothing...

Here is what i've tryed:

private void Form1_Load(object sender, EventArgs e)
{
    webView.Source = new Uri("file://C:/Users/xxx/Desktop/VisualSelf/self.html");
}

Solution

  • You can read HTML file , and then NavigateToString

    if (webView!= null && webView.CoreWebView2 != null)
    {
         string text = system.IO.File.ReadAllText(@"C:/Users/xxx/Desktop/VisualSelf/self.html");
         webView.CoreWebView2.NavigateToString(text);
    }
    

    or you can Navigate to local file:

    webView.CoreWebView2.Navigate("file:///C:/Users/xxx/Desktop/VisualSelf/self.html");
    

    Also, you need to install Microsoft Edge WebView2 Runtime.