tags:

views:

620

answers:

1

I have a .resx file which contains some string resources for my project. At runtime, I'd like to read all of the resources defined in this file and store them in a container. The reason isn't important.

I've read about the ResourceReader class, which operates on binary .resource files. My impression is that the .resx is converted to binary format on build and embedded in the assembly. Is this true, or do I need to manually generate it with resgen? If I do need to generate it manually, how do I add it to my VS project, embed it in the assembly, and access it at runtime?

In short, how can I access the resources defined in a .resx at runtime using a ResourceReader?

+2  A: 

You can use ResourceReader.GetEnumerator() to get an enumerator to your resources.

If you're tryign to read .resx files directly, you may want to consider using ResXResourceReader instead of ResourceReader.

If you're trying to read from compiled in .resources (resx -> resources), you'll need to modify the name appropriately. Say you name you .resX file "MyRes.resx", you'll want to use:

var resManager = new ResourceManager("MyRes.resources");

If the resx file is in a satellite assembly, though, it'll add the project name, ie: ("MyProject.MyRes.resources");

If you're using localization, you'll need that, too: ("MyProject.MyRes.en-GB.resources");

Reed Copsey
Yes, but first I need to construct the ResourceReader by passing it the path of a .resource file. I have created a .resx file, but I don't understand where the .resource file comes from.
Odrade
I've thought of simply generating one with resgen and adding it to my project, but I thought that the binary resource was supposed to be embedded in the assembly. I must be misunderstanding something.
Odrade
The .resource file is generated at build time by the resource file generator: http://msdn.microsoft.com/en-us/library/ccec7sz1(VS.80).aspx
Lucero
@David: I added more details
Reed Copsey
By default, VS dumps the .resource file in obj/release. Is there any way to change this behaviour?Reed: Thanks for the more detailed reply.
Odrade
@David: The resources are linked into the executable itself. They're compiled in obj/release, but then (later) bundled into the assembly. The code above should just work.
Reed Copsey
Just to add to the answer, you can load embedded resources using the stream.. var asm = Assembly.GetExecutingAssembly(); var name = asm.GetManifestResourceNames(); var stream = asm.GetManifestResourceStream(name[0]); ResourceReader reader = new ResourceReader(stream);
Andrew Keith