tags:

views:

471

answers:

4

Hi, I created a DLL for encapsulating my Images and after that I want to get image names from DLL as a list. Before posting this post I googled about it and I saw an example that is below.

public static List<string> GetImageList()
{
    List<string> imageList;            
    System.Reflection.Assembly BOAUIResources = System.Reflection.Assembly.GetExecutingAssembly();
    string[] resources = BOAUIResources.GetManifestResourceNames();
    return resources.ToList<string>();
}

This code just accessing image names that build action property is "embedded resource". because of accessing in WPF, my images build action type must define as "resource".

So How can I list image names, that build action property is defined as resource, from DLL ?

+2  A: 

Image resources can be added to an assembly in a couple of different ways, that will have some impact on the code to enumerate the image names.

  • You can add images to a resx file.
  • You can add the images directly to the solution (as with your code files), and set their build action to 'Embedded Resource'.

The code sample that you supplied in your question will work in the second case. Note however that it will also list any other manifest resources (such as embedded resx files) and not just your images.

If you have added the images to a resx file you can enumerate resources using a ResourceSet obtained from a ResourceManager:

// This requires the following using statements in the file:
// using System.Resources;
// using System.Collections;

ResourceManager rm = new ResourceManager(typeof(Images));
using (ResourceSet rs = rm.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true))
{
    IDictionaryEnumerator resourceEnumerator = rs.GetEnumerator();
    while (resourceEnumerator.MoveNext())
    {
        if (resourceEnumerator.Value is Image)
        {
            Console.WriteLine(resourceEnumerator.Key);
        }
    }
}

In the first line, where it says ResourceManager(typeof(Images)), you will need to exchange Images with the name of the resource file i which your images are located (in my sample, it was called "Images.resx").

Fredrik Mörk
A: 

Hi Mr Mörk,

Firstly thank you for your reply.

You talked about two way to adding images into an assembly. Yes of course if we add images into resx file your code will work. But we are preparing User Interfaces with Windows Presentation Foundation and we can access images only added as a "resource" into assembly.

In this circumstances, is there any way to enumarate images that added as "resource" into assembly ?

okkesemin
If I understand your question correctly (you add the image files as solution items in Visual Studio, and set their build action to embedded resource), the code example in your question should list the image names (but not only them; it will also list embedded resx files).
Fredrik Mörk
Fredrik, an "embedded resource" is different from a "resource" when it comes to possible build actions. This naming that Microsoft has chosen causes a lot of confusion and makes it very difficult to make relevant searches on the web on this topic but the two are very different beasts.
jpierson
+1  A: 

Try this. (Taken from the book - Programming WPF By Chris Sells, Ian Griffiths)

 public static List<string> GetImageList()
            {
                System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
                System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture;
                string resourceName = asm.GetName().Name + ".g";
                System.Resources.ResourceManager rm = new System.Resources.ResourceManager(resourceName, asm);
                System.Resources.ResourceSet resourceSet = rm.GetResourceSet(culture, true, true);
                List<string> resources = new List<string>();
                foreach (DictionaryEntry resource in resourceSet)
                {
                    resources.Add((string)resource.Key);
                }
                rm.ReleaseAllResources();
                return resources;
            }
SaveseR
Works great! Thanks for sharing this example.
jpierson
A: 

Thank you SaveSer. This is the answer.

okkesemin