Bitcoin Blogger
Bitcoin Blogger

Reputation: 61

C#: How to load an internal .html file resource into a webbrowser control?

I got the file named test.html which is just a basic html file with some text in it. The test.html is a resource in my c# project, and I got a webbrowser named webbrowser1 that needs to load my html file.

So how to load the test.html into my webbrowser

I tried this, but it doesn't work:

private void button1_Click(object sender, EventArgs e)
{
     webBrowser1.DocumentStream = 
         Properties.Resources.ResourceManager.GetStream("test.html");
}

Any solutions please?

Upvotes: 6

Views: 12112

Answers (2)

Mike
Mike

Reputation: 19

Here is my solution for this problem. I needed a way to make a reusable quick popup help, besides making several external html files to launch.

  1. I added the html files into the project resource.

    enter image description here

  2. I will use the Capacities.html file as my example.

    <!DOCTYPE html>
    <html lang="en">
    
        <style>
    
        #mainHelp {
            width:500px;
            margin:0 auto;
    
        }
    
        </style>
    
        <div id="mainHelp">
            If desired, &quotFluid Capacities&quot can be set for each fluid (Water, Oil, Fuel) and side (Port, Stbd).
              If capacities are set to '0', this disables reporting of the capacities.  The capacities are whole number values,
               but can be entered as either liters or gallons.
    
        </div>
    </html>
    
  3. In my QuickHelpView.xaml.cs, to make the window reusable, I send a string of the caller to direct to the resource needed. In this case, the "capacity" was needed.

    public void SetupQuickHelp(string _helpSelected)//, FileDataAccess aFilePathAccess
    {
        //filePathAccess = aFilePathAccess;
    
        string htmlText = "";
    
        if (_helpSelected == "instance")
        {
            htmlText = Properties.Resources.InstanceSettings;
        }
        else if (_helpSelected == "engine")
        {
            htmlText = Properties.Resources.EngineHours;
        }
        else if (_helpSelected == "capacity")
        {
            htmlText = Properties.Resources.Capacities;
        }
    
        webBrowserView.NavigateToString(htmlText);
    }
    
  4. When run and the help file was selected, the following was displayed.

    enter image description here

Upvotes: 0

Mikko Viitala
Mikko Viitala

Reputation: 8394

I think "test.html" is not valid name for resource. Try using "test_html" instead. Then the following works just fine.

private void button1_Click(object sender, EventArgs e)
{
     string html = Properties.Resources.test_html;
     webBrowser1.DocumentText = html;
}

So if HTML file is

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <title></title>
</head>
<body>
  This is some resource HTML
</body>
</html>

You'll end up with

enter image description here

Upvotes: 10

Related Questions