views:

202

answers:

2

I have some resources in a C# Assembly which I address by

byte[] foob = Properties.Resources.foo;
byte[] barb = Properties.Resources.bar;
...

I would like to iterate through these resources without having to keep an index of what I have added. Is there a method that returns all the resources?

+1  A: 

Try Assembly.GetManifestResourceNames(). Call it like this:

Assembly.GetExecutingAssembly().GetManifestResourceNames()

Edit: To actually get the resource call Assembly.GetManifestResouceStream() or to view more details use Assembly.GetManifestResourceInfo().

J. Random Coder
+2  A: 

EDIT: It turns out they're properties rather than fields, so:

foreach (PropertyInfo property in typeof(Properties.Resources).GetProperties
    (BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
    Console.WriteLine("{0}: {1}", property.Name, property.GetValue(null, null));
}

Note that this will also give you the "ResourceManager" and "Culture" properties.

Jon Skeet
isn't it slower than accessing directly the resources through GetManifestResource* ?
Thomas Levesque
They do different things. For one thing, you may have content in the assembly which isn't in `Properties.Resources`, and secondly this gives you individual resources instead of resource *files*. If you have two string resources, for example, they may both be in the same resource file - finding the resource names means loading the file and then asking for the names within that file etc.
Jon Skeet
In other words, this answer does what the question specifically asked for :)
Jon Skeet