6

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?

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
Bitcoin Blogger
  • 61
  • 1
  • 1
  • 4
  • possible duplicate of [Load local HTML file in a C# WebBrowser](http://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser) – MethodMan Jan 30 '15 at 16:47
  • Did you check this? http://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser – user1462616 Jan 30 '15 at 16:54
  • I did checked all of those but doesnt work non of them works for me... I mean my resource is embedded in the solution, not on the hard disk! So it doesnt work like that. – Bitcoin Blogger Jan 30 '15 at 18:18

2 Answers2

10

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

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
0

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

tdy
  • 36,675
  • 19
  • 86
  • 83
Mike
  • 19
  • 3