Rocshy
Rocshy

Reputation: 3509

access active assembly resources

I am trying to dynamically load images from the Resources folder but for some reason GetManifestResourceStream returns null. This is what I wrote:

System.Reflection.Assembly asm;
asm = System.Reflection.Assembly.GetExecutingAssembly();
Bitmap bmp = new Bitmap(asm.GetManifestResourceStream("MyProject.Properties.Resources.Image.png"));

I've tried every method I found here, but it doesn't work. The path is correct, the file is there. I need to do this dynamically because depending on user permissions, different assemblies will be loaded. So basically I need two things:

  1. Get a list with all the assemblies that were loaded
  2. Load an image corresponding to the loaded assembly into a Bitmap

How can this be done?

Upvotes: 1

Views: 214

Answers (1)

akton
akton

Reputation: 14386

This usually means the resource name is incorrect. Load the assembly in ildasm, look at the manifest for ".mresources" sections and find the one you want to load.

If not, the call to GetExecutingAssembly returns the assembly containing the code currently executing, which may be different to the DLL that contains the resource. Use GetCallingAssembly to load it from a separate DLL instead.

To answer your questions above:

  1. Get a list with all the assemblies that were loaded

Use the AppDomain.GetAssemblies method, for example:

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  1. Load an image corresponding to the loaded assembly into a Bitmap

Iterate through the assemblies looking for the resource using GetManifestResourceStream.

Upvotes: 1

Related Questions