10

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");
}
Kasper Juner
  • 832
  • 3
  • 15
  • 34

1 Answers1

9

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.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
NajiMakhoul
  • 1,623
  • 2
  • 16
  • 30