2
votes

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");
}
1
i've added the html file from desktop to my project but if i try to do : new Uri("ms-appx-web:///VisualSelf/self.html") the webView still show nothing - Kasper Juner
May you try to load a universal website e.g microsoft.com? - m.r226
@m.r226 no... if i set webView.Source = new Uri("microsoft.com/it-it/") it still doesn't show nothing - Kasper Juner

1 Answers

5
votes

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.