Reputation: 235
I am trying to open a HTML file that is embedded inside a winform. If I try google, it works if try
WebReadmeText.Navigate(@"www.google.com");
But what I would like to do is open a winform that does then brings up a html page that allows the user to read the Readme file. The files are stored in a local folder from within the project called ReadmePages and the file is Readme1.html.
I have placed the code inside the constructor and the code which is as follows:
public Readme()
{
InitializeComponent();
WebReadmeText.Navigate(@"/ReadmePages/Readme1.html");
}
The exact path that I am using is as follows
C:\Users\Keith\source\repos\Triangle\ReadmePages
When the program is complied this is the output.
To be on the safe side, the HTML code is as follows:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Readme Volume 1</title>
</head>
<body>
Triangle Version 1.<Br />
The longest journeys start with the smallest step, a wise man once said.<Br />
<Br />
The First Step.<Br />
This is a simple program where the Pythagoras' Theorem is calculated and the output displayed.<Br />
Everyone can remember from school the square of hypotenuse is equal to the sum of the squares of the adjacent and the opposite.<Br />
In this version I am proofing the formula and to make sure that the theory is correct.<Br />
<Br />
In this version I am not making the program object orientated or making it efficient, as I have said it is to validate the proof of concept.<Br />
The amount of moving parts is zero.<Br />
The difficulty level is one.<Br />
<Br />
</body>
</html>
The HTML output is as follows
Thanks for any help in advance
Upvotes: 1
Views: 1718
Reputation: 631
I think the problem is, that the WebBrowser
component does not understand that relative path.
So my guess is, you have to add the full path parameter, like this:
WebReadmeText.Navigate(@"C:\Users\Keith\source\repos\Triangle\ReadmePages\Readme1.html");
In your case, this could work too, and you dont need to hard code the full path:
var baseDir = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
WebReadmeText.Navigate($@"{baseDir}\ReadmePages\readme1.html");
Upvotes: 2
Reputation: 3638
You should expect to open content file from the assembly output folder and not from project folder. For instance bin\debug\ReadmePages\Readme1.html. Achieving this is easy provided you've added Readme1.html along with its parent folder ReadmePages part of the project and have marked Readme1.html property "Copy to Output Directory" as "Copy if Newer". Then you can use following code (regardless of the build configuration – debug/release/ETC.)
var executablePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
WebReadmeText.Navigate($@"{executablePath}\ReadmePages\Readme1.html");
Upvotes: 1