Wednesday, September 26, 2007

Navigating to an embedded resource in .NET

This task is very common but since i did not find a clear and simple solution i decided to write it.

I have a simple C# application which has a webbrowser control and i want to use the res:// protocol to navigate to an embedded resource, a html file from the assembly, like this:


since managed resources are not native ones, adding an html file to the project and select "embedded resource" wont help.
if you open the assembly with a resource editor you won;t see the html resource.
hence, the res:// protocol wont work.

so i wrote this simple console application which accepts as parameters the resource file you want to add.

AddResourceManaged.exe ExePath ResFile ResName [ResType]

adds a new resource to an exe file
ExePath - path of the exe
ResFile - path of the file to be added as a resource
ResName - name of the resource
ResType - (optional) type of the resource; if omitted, the application will try to use one of the default resource types
based on the ResFile file extension

AddResourceManaged.exe myManagedApp.exe Help.htm help.htm 23

it is equivalent to:

AddResourceManaged.exe myManagedApp.exe Help.htm help.htm
because the value of MAKEINTRESOURCE(RT_HTML) is 23

No comments: