2

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?

Community
  • 1
  • 1
Ronald Jeremiah
  • 65
  • 1
  • 3
  • 8

2 Answers2

2

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.

Josh
  • 10,352
  • 12
  • 58
  • 109
  • I keep coming up with a parsing error when it looks at the first image in the resx file I have it look at. – Ronald Jeremiah Jul 26 '12 at 10:23
  • string resxFile = @"//File path ending in Theme1Images.resx"; using (ResXResourceReader resxReader = new ResXResourceReader(resxFile)) {foreach (DictionaryEntry entry in resxReader) {string resourceKey = entry.Key.ToString(); object resource = entry.Value; MessageBox.Show("message");} } This is all in a try-catch: ArgumentException was unhandled ResX file Could not find a part of the path '//FilePath\bin\resources\1336578195503.jpg'. Line 123, position 5. cannot be parsed. – Ronald Jeremiah Jul 26 '12 at 16:16
  • @Jamz And that file exists in the \bin\resources folder? – Josh Jul 26 '12 at 16:26
2

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

Cliveburr
  • 141
  • 1
  • 9