1

I'm trying to create a barebones WebView2 app that lets me load a specific HTML file from the same directory as wherever the app is located (so if it's on the desktop, installed in the C:\Windows\ directory, C:\Users\Poopy\, wherever. The HTML file will be titled main.html (Or I'll change it to index.html)

Still, how can I get whatever the current directory is, and create a file path to load when the app starts?

After some poking around, this is what my Form1.cs has:

if (webView != null && webView.CoreWebView2 != null) {
    string currentPath = Directory.GetCurrentDirectory();
    string text = System.IO.File.ReadAllText(currentPath + @"\main.html");
    webView.CoreWebView2.NavigateToString(text);
    this.webView.Source = new System.Uri(currentPath + @"\main.html");
}

What I expect to happen: The blank file, in the same folder as the app, is displayed. In the case of main.html, a white page with the text "hello world" should be displayed. I will be able to pull up the dev tools popup with F12.

What actually happens: If no source is specified in the properties, I get a blank screen with NO web page displaying. It appears no WebView form shows up; If I press F12 no dev tools pops up. If a source is specified, and it's a valid entry, I'll load the URL, or if I enter just the filename (assuming it defaults to a relative URL with just file:///main.html entered) I get a ERR_FILE_NOT_FOUND error page instead.

  • 2
    The code `webView.CoreWebView2.NavigateToString(text);` present in the question should work. I'd recommend re-reading [MCVE] guidance on posting code and [edit] the question accordingly. I.e. if you have problem *showing* HTML content - remove all file related code and set `text` to some constant, if you have trouble locating files - adjust code accordingly and explain what you see as results/expect as results. – Alexei Levenkov Oct 22 '21 at 06:58
  • 1
    Have you tried setting Source to `file:///c:\temp\default.html` (replace with actual path) - notice the 3 `/` – Tu deschizi eu inchid Oct 22 '21 at 13:55
  • I'll try that when I get home (got a friend's birthday to go to after work). I was using `file:\\\ ` instead. – Anthony LoPrimo Oct 22 '21 at 14:01
  • @AlexeiLevenkov So I tried that WITHOUT the `this.webView.Source` line and it still doesn't show anything at all - not even an error, acting as if the WebView form didn't exist in the app. – Anthony LoPrimo Oct 22 '21 at 14:15
  • Does this answer your question? [Loading html file from local folder into webview](https://stackoverflow.com/questions/10223674/loading-html-file-from-local-folder-into-webview) – mtk Oct 25 '21 at 01:24
  • 3
    @mtk: `WebView` isn't the same as `WebView2`. The documentation for `WebView2` is here: https://learn.microsoft.com/en-us/microsoft-edge/webview2/webview2-api-reference – Tu deschizi eu inchid Oct 25 '21 at 02:09

1 Answers1

0

The SetVirtualHostNameToFolderMapping function can help with this. It lets you serve files from a folder to WebView2, and note this bit from the docs:

Relative paths are interpreted as relative to the folder where the exe of the app is in.

So something like this should do the trick:

webView.CoreWebView2.SetVirtualHostNameToFolderMapping(hostName: "mycoolapplication",
                                                       folderPath: "",  
                                                       accessKind: CoreWebView2HostResourceAccessKind.Allow);

webView.CoreWebView2.Navigate("https://mycoolapplication/main.html");
Reilly Wood
  • 1,753
  • 2
  • 12
  • 17