« MSDE Setup & Installers, SCM.exe, Part 3 | Main | Remember to install an Application.ThreadException event handler »

Embedded Resources

Resource files can be made available to an assembly by marking their Build Action property to “Embedded Resource”.

 

If “license.rtf” is in a subfolder “Resources” of an assembly whose default namespace is “Kizmet.JobBlaster”,

the following code will recover the resource as a string:

 

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();

string rsrcName = “Kizmet.JobBlaster.Resources.license.rtf”;

System.IO.Stream s = a.GetManifestResourceStream(rsrcName);

System.IO.StreamReader sr = new System.IO.StreamReader(s);

string license = sr.ReadToEnd();

 

The example code in the documentation builds the resource name from the assembly’s name:

 

string rsrcName = a.GetName().Name + ".Resources.license.rtf";

 

This doesn’t seem to work in general, since the assembly name can differ from the default namespace.

 

Use ILDASM to view the assembly manifest when in doubt about what a resource is being named.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)