views:

206

answers:

1

I am working on a Windows Forms application in C#/.Net. I want to use a resource file that contains translations of my strings. My my project in visual studio I have the following hierarchy:

  • Project
    • CS files ...
    • Resources\
      • resource.en-US.resx

I am trying to read in the resource file as follows:

m_ResourceReader = new ResXResourceReader("resources/resource.en-US.resx");

When I run this project, Visual Studio seems to look for the resources folder in the bin/Debug output folder of my project.

My questions are:

  • What is the right way to reference a resource file?
  • I would like my installer to place this resource file under my application's folder under Program Files\MyApp\resources\resource.en-US.resx. What would be the way to make ResXResourceReader read it from that location.

Thanks for your help.

-Raj

+1  A: 

There is no right way to reference a .resx file at runtime. They are design-time files, the build system translates them to satellite assemblies. And copies the resulting DLL into the en-US folder of your bin\Debug folder. And the runtime automatically finds and uses them if the current culture is set to en-US.

I suppose you can get .resx reading going, but you'll get no help from the IDE or the build system to do so. I'd have to recommend you avoid fighting the machine.

Hans Passant
Hans, Thanks for your response. What is the right way to reference the keys and values in the resx at runtime?
There is no right way, you can only enumerate them. GetEnumerator() returns an IDictionaryEnumerator. Lookup is O(n).
Hans Passant