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.