Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS WebBrowser + Embedded HTML Resource + res:// Protocol

I have an embedded HTML resource (helloworld.htm) inside my Visual Studio project. (Ie, I've added an HTML file to the project and set its properties to "Embedded Resource".

Within the same application I have a WebBrowser control.

I'd like to direct the WebBrowser control to display the HTML resource using the res:// protocol.

But I can't figure out the exact format needed to address an embedded resource using this style of URL.

Any ideas? Thanks!

like image 259
user144051 Avatar asked Aug 10 '09 12:08

user144051


2 Answers

I know this thread is dead, but I had to do this yesterday and couldn't get any of these methods to work. So I did a little research and found the method below, using the Stream class. I thought I'd post it here just in case somebody else runs into the same nonsense:

Stream docStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.HTMLPage.html");
WebBrowser.DocumentStream = docStream;

This worked for me without any tinkering, and it was so simple. I hope it benefits somebody else!

like image 60
Chris Barlow Avatar answered Sep 28 '22 07:09

Chris Barlow


The res: protocol is not dead and is still a great way to embed webpages into Windows applications using a WebBrowser control. Unfortunately, it seems to me there are two types of resources in exe and dll files out there: C resources and .net resources. It may possible to embed C resources in a .net dll but I haven't figured out how to yet.

To answer your question, the res protocol is documented at here but actually building the dll or exe is the tricky part. The res protocol is simple enough. The basic gist of it is you specify res://, follow that by the path to the executable or dll (just the dll name if it's in the current path). For HTML type resources, follow that with the filename. Here is a recent MSDN article the talks about some known problems with the res protocol: http://support.microsoft.com/kb/220830.

Building the dll or exe resources can be a little bit tricky. For easiest results, make all of your resources of type HTML (even your .js, .png, .jpg files). Instead of naming your resources with a #defined resource identifier, modern res files allow you to name the files with a string. Doing this will make your life a lot easier.

Advanced Tip: Having folder names in the resource name is tricky; I haven't figured it our yet. I think you may be able to simulate folders by putting slashes in the resource name, but I think res protocol gets confused by the slashes thinking the first part of the path is the resource type. Explicitly specifying the resource type may alleviate this.

Advanced Tip 2: For the path newer versions of IE can deal with the '\' character, but you can use '%5C' as a substitute for '\' if you need to specify the absolute or relative location of the dll or exe.

Additional Resource:
MSDN Social: Webbrowser and res: protocol
DelphiDabbler: How to create and use HTML resource files

like image 24
Tim Ludwinski Avatar answered Sep 28 '22 08:09

Tim Ludwinski