AustinC AustinC - 1 year ago 77
C# Question

Looping through a file of resources and loading them into an array

I am writing a program that calls for me to load in a series of images. I've created a file of images which I've embedded into the C# project. However, I haven't been able to find any suitable answer that will allow me to loop through these images and load them all into a data structure.

Is there a way to do this? I feel like this problem should require a simple fix, and that I may be overthinking it. Any help is appreciated, thanks!

In the interest of making my question any less vague, I'm trying to access my resource file named "GameBoardImages", which contains all of my images. I can access them one at a time, but have yet to figure out how to implement some mechanism that will allow me to loop through the GameBoardImages file and access and/or collect these images in order to store them into a data structure.

Using the assembly.GetManifestresourceNames() method, I get a list of resources such as: MyNameSpace.GameBoardImages.0800_GameBoardImage.png, MyNameSpace.GameBoardImages.0900_GameBoardImage.png, MyNameSpace.GameBoardIamges.1000_GameBoardImage.png, etc.

How do I loop through the entries in MyNameSpace.GameBoardImages?

Answer Source

Give this a shot.

var images = Assembly.GetExecutingAssembly()
                     .Where(x => x.EndsWith("_GameBoardImage.png"))

foreach (var img in images)
    // Do stuff...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download