Resource Protocol in .NET

I never liked deploying an application that had numerous dependencies as there just seemed to be more room for error. When I discovered the resource protocol (res://), it seemed like a great way to embed HTML files I needed to distribute with my Outlook add-in. The nice part is that there is a single file deployed to the user’s system and you avoid any issues of users accidentally removing dependency files or similar headaches. I originally thought it would be as easy as adding my HTML file as a resource in my Visual Studio project, but come to find out that would create a managed resource (resx:// protocol). Unfortunately, the resx protocol has two glaring limitations:

From MSDN (https://msdn2.microsoft.com/en-us/library/bb189724.aspx):

The resx:// protocol is designed to load resources from .resx files that are embedded into managed assemblies, with the following limitations:

  • The resx:// protocol is used to load resources from a managed assembly, which must be strong-name signed and located in either %windir%\ehome or the global assembly cache (GAC).
  • In the syntax resx://myassembly/myresourcefile/myresourceidentifier, the myassembly parameter does not have a file extension, so you cannot have a managed DLL and EXE with the same name and be able to load resources from both of them.

So I set out on a quest to figure out how to embed a HTML file into my DLL and access it using the resource protocol. It actually isn’t that difficult, but the documentation is scarce. Thanks to Wouter van Vugt’s post (http://tinyurl.com/34pezy), I was able to stumble my way through it. Here are the steps:

1. Edit your project file (.vbproj or .csproj) and add the following:
<PropertyGroup>
<Win32Resource>NameOfResourceFile.res</Win32Resource>
</PropertyGroup>

2. Create a resource script file named NameOfResourceFile.rc and add it to your project. It should look something like this:
/* standard resource # for HTML */
#define HTML 23

/* include all resources from external files */
RES_REF     HTML     “NameOfHTMLFile.html”

/* end */

3. Add the HTML file named NameOfHTMLFile.html to your project.

4. Add the following to the prebuild commandline (Project Properties > Compile > Build Events):
“C:|Program Files|Microsoft Visual Studio 8|SDK|v2.0|Bin|rc” /r “$(ProjectDir)NameOfResourceFile.rc”

5. Compile you project.

6. Test it. Put this in a browser to load the file: res://NameOfDLL.dll/RES_REF

You can also embed graphics into the resource dll and reference them in the embedded HTML file. Note that you need to specify the resource type in the URL if it is not a HTML resource (the default). For example, if you embed a bitmap, you would reference it like so: res://NameOfDLL.dll/2/NameOfBitmapFile.bmp. You can find a list of common resource types here: http://msdn2.microsoft.com/en-us/library/aa365064.aspx. Images don’t have an associated resource type, so you would use res://NameOfDLL.dll/GIF/NameOfGIFImage.gif to access the image. Also, you can’t use the resource protocol to access .xsl or .xml files.

Leave a Reply

Your email address will not be published. Required fields are marked *