Title: Loading resources from an external assembly
Slug: loading-resources-from-an-external-assembly
Date: 2007-03-24 14:59:00
Author: Kartones
Lang: en
Tags: Development, C#, .NET
Description: How to load resources from an external C# assembly.

 <p>Loading resources from a resource file helps a lot. But loading them from an external assembly is a lot more helpful, because you can update the content without having to recompile the entire solution, just the resources assembly.</p>
<p>This is an easy way of manipulating an external assembly that has a resource file embedded:</p><font size="4" color="#2b91af">
</font><p><font size="4" color="#2b91af"><font size="2" face="courier new,courier">ResourceManager</font></font><font face="courier new,courier"> resManager = <font color="#0000ff">null</font></font><font face="courier new,courier">;<br><font color="#2b91af">ResourceSet</font> resSet = <font color="#0000ff">null</font></font><font face="courier new,courier">;<br><font color="#2b91af">Assembly</font> resAssembly = <font color="#0000ff">null</font>;</font></p><font face="courier new,courier"></font>
<p><font face="courier new,courier"><font color="#008000">// Load an assembly located on same dir as executable and named ImageAssembly.dll<br></font></font><font face="courier new,courier">resAssembly = </font><font color="#2b91af" face="courier new,courier">Assembly</font><font face="courier new,courier">.Load(<font color="#a31515">"ImageAssembly"</font>);</font></p>
<p><font face="courier new,courier"><font color="#0000ff"><font color="#008000">// Get all embedded resource files the assembly has inside</font><br>string</font>[] resourceFiles = resAssembly.GetManifestResourceNames();</font></p><font face="courier new,courier"></font>
<p><font face="courier new,courier"><font color="#008000">// We assume it has one resource file (error handling should go before this)<br>// Beware of the bold fragment: Manifest gives the full namespace + resource name, but constructor<br></font></font><font face="courier new,courier"><font color="#008000">// accepts just the name without full namespace</font><br>resManager = </font><font face="courier new,courier"><font color="#0000ff">new</font> <font color="#2b91af">ResourceManager</font>(resourceFiles[0].Remove(<b>resourceFiles[0].LastIndexOf(<font color="#a31515">'.'</font></b></font><font face="courier new,courier"><b>))</b>, resAssembly);</font></p>
<p><font face="courier new,courier"><font color="#008000">// Get all resources inside the resource file</font><br>resSet = resManager.GetResourceSet(System.Threading.</font><font face="courier new,courier"><font color="#2b91af">Thread</font>.CurrentThread.CurrentUICulture, <font color="#0000ff">true</font>, <font color="#0000ff">true</font>);</font></p><font face="courier new,courier"></font><font color="#2b91af">
</font><p><font color="#2b91af"><font face="courier new,courier"><font color="#008000">// And lastly, do whatever we want with them</font><br>IDictionaryEnumerator</font></font><font face="courier new,courier"> enumerator = resSet.GetEnumerator();<br><font color="#0000ff">while</font></font><font face="courier new,courier"> (enumerator.MoveNext())<br>{<br>    <font color="#008000">// In this example, we want only images in supported formats</font><br><font color="#0000ff">    if</font> (enumerator.Value <font color="#0000ff">is</font> <font color="#2b91af">Bitmap</font></font><font face="courier new,courier">)<br>    {<br></font><font face="courier new,courier"><font color="#008000">        // ...<br></font>    }<br>}</font></p>
