4

Well in my windows forms application i have a simple web browser which is supposed to run a .html file containing a css styled registration form. It is running very well when loading the page remotely using webBrowser1.Navigate("http://www.example.com/mypage.html");; However, When i tried to embed the .html file [along with the css files required] in the resources by setting the Building Action to Embedded Resource, the webBrowser only navigates to the .html file while not detecting the css files, which result in a creepy page look.

Here are the codes i have tried so far.

The .html file :

  <link href="lang/en/compact.css" rel="stylesheet" type="text/css" />
  <link href="lang/en/lang.css" rel="stylesheet" type="text/css" />
  <link href="img/basics.css" rel="stylesheet" type="text/css" />

How i get the .html file from the Embedded Resources :

using (Stream stream = Assembly.GetExecutingAssembly()
                   .GetManifestResourceStream("DummyBrowserFormImprovements.HTML." + "mypage.html"))
    if (stream != null)
        using (var reader = new StreamReader(stream))
        {
            string result = reader.ReadToEnd();
            dummyBrowser1.DocumentText = result;
        }

Unfortunately, The above approach only loads the .html file without recognizing the css files referenced in it; Hence, any detailed answer about how to embed a .html file that uses js, css external files in windows forms and how to load it into a webBrowser would be very appreciated. I hope i was clear in representing my problem and thanks in advance.

Roman Ratskey
  • 5,101
  • 8
  • 44
  • 67

1 Answers1

1

The problem is likely because the other files are still embedded in your application. Where is the browser going to find them?

You could write all the required embedded files to temporary directory, and have your web browser load the .html page from there. Once the html page is loaded it should be able to find your other resources since they will be in the same directory (or whatever structure you make).

Basically I am saying that you could extract all your files into a temp directory structure like:

C:\mytempsite\mypage.html
C:\mytempsite\lang\en\compact.css
C:\mytempsite\lang\en\lang.css
C:\mytempsite\img\basics.css

Then you can set the web browser to navigate to "C:\mytempsite\mypage.html"

As long as you can navigate to this directly without your application, and the page loads correctly, then this should work from your application when it navigates there. Just make sure mypage.html has well formed html and is a complete page.

Sam Plus Plus
  • 4,381
  • 2
  • 21
  • 43
  • This is not working i have tried this the browser the webBrowser.DocumentText = File.ReadAllText(@"C:\mypage.html"); [With all the needed resources] it also can't load them...' – Roman Ratskey Apr 16 '13 at 15:16
  • Is there any other way to directly run it from the resources without having to extract it ? – Roman Ratskey Apr 16 '13 at 15:27
  • Not that I am aware of, maybe someone else can think of away, but logically, I do not understand how the web browser control could find the css files if they are still embedded in your application. The only other way I could think to do that is to have all your css and other files as inline code. Then you only have one file and could set your web browser DocumentText to be the text of that one file. – Sam Plus Plus Apr 16 '13 at 15:31
  • There might be a way so i detect when the browser requests the css file then i respond to the browser with the embedded resource file. – Roman Ratskey Apr 16 '13 at 15:55
  • Check out this question http://stackoverflow.com/questions/1254605/ms-webbrowser-embedded-html-resource-res-protocol it seems to have a lot of information about using the res:// protocol This might be what you need. – Sam Plus Plus Apr 16 '13 at 15:59