Ronald Jeremiah
Ronald Jeremiah

Reputation: 65

Looping through a set of image resources (.resx)

I've got a 3 sets of 9 images in seperate .resx files, and I'm trying to figure out how to loop a single set into 9 static picture boxes.

Loop through all the resources in a .resx file

I've looked through some of the solutions in the above link, like using ResXResourceReader, but it comes up with a parsing error when I use the GetEnumerator method.

When I use the ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); line, there's no definition for the ResourceManager within the Form class, or a GetResourceSet method when I create my own ResourceManager.

There is actually a method called CreateFileBasedResourceManager which I've dabbled in, but truth be told I don't understand the parameters it needs too well aside from the directory.

I've also looked at some of the solutions involving assemblies and retrieving the executing image assembly at runtime, but I think that's a little out of my depth at the moment.

Can anyone tell me what I'm doing wrong with the first two methods or maybe something entirely different?

Upvotes: 2

Views: 3189

Answers (2)

Cliveburr
Cliveburr

Reputation: 141

I known that this is a old question, but today I got the same problem, and solve setting the BasePath property, like this:

oResReader = new ResXResourceReader(strInputFile);
oResReader.BasePath = Path.GetDirectoryName(strInputFile);

I found this solution here

Upvotes: 2

Josh
Josh

Reputation: 10604

Looking at MSDN, you should be able to iterate the values from a RESX file like so:

string resxFile = @".\CarResources.resx";


// Get resources from .resx file.
  using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
  {
     // Retrieve the image.
     Object image =  resxSet.GetObject("NAMEOFFILE", true);
  }

If you wanted to iterate all objects in the RESX file, you could do something like this:

using (ResXResourceReader resxReader = new ResXResourceReader(resxFile))
  {
     foreach (DictionaryEntry entry in resxReader) {
        // entry.Key is the name of the file
        // entry.Value is the actual object...add it to the list of images you were looking to keep track of
     } 
  }

More can be found here.

Upvotes: 2

Related Questions