Loading resources from an external assembly

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.

This is an easy way of manipulating an external assembly that has a resource file embedded:

ResourceManager resManager = null;
ResourceSet resSet = null
Assembly resAssembly = null;

// Load an assembly located on same dir as executable and named ImageAssembly.dll
resAssembly = Assembly.Load("ImageAssembly");

// Get all embedded resource files the assembly has inside
[] resourceFiles = resAssembly.GetManifestResourceNames();

// We assume it has one resource file (error handling should go before this)
// Beware of the bold fragment: Manifest gives the full namespace + resource name, but constructor
// accepts just the name without full namespace
resManager =
new ResourceManager(resourceFiles[0].Remove(resourceFiles[0].LastIndexOf('.')), resAssembly);

// Get all resources inside the resource file
resSet = resManager.GetResourceSet(System.Threading.
Thread.CurrentThread.CurrentUICulture, true, true);

// And lastly, do whatever we want with them
enumerator = resSet.GetEnumerator();
// In this example, we want only images in supported formats
if (enumerator.Value is Bitmap
// ...


Posted by Kartones on 2007-03-24