Compile a Resource file and get its Value

It’s very easy however, if you don’t know the rule then, it can cause quite a bit of trouble initially.
First of all merely adding a resource file to your application and debugging it won’t help. To use the resource file we need to compile it first using the resgen command. If your resource file is not in the start up project then you have to copy the binary output to the bin directory of the startup project.
This step is very important because if its not in the bin directory of the Start up project then, the file won’t be accessible.To compile a resource file do the following:Name of the resource file: resFile.en-US.resx
Output file Name: resFile.en-US.resourcesOpen Visual Studio Command Prompt.
Navigate to the directory of your resource file and run the following command:

resgen resFile.en-US.resx resFile.en-US.resources

The binary file has been generated at the same location where the resource file is.

For the sake of simplicity, you can add a post-build event for this conversion in your Visual Studio. It will save you from the pain of manually compiling the file whenever something has been changed in the file. However, remember, ResGen is not in the path environment variable, so you can either put it in or just reference ResGen with an absolute reference:
In win7 the resgen.exe can be found at the following location:


"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\resgen.exe"

Now you can easily access the resource file by running the following command


string resourceName = "resFile";
string resourcePath = @".\\";
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(resourceName, resourcePath, null);

To read the values from the resource file, you can use the getstring method


rm.GetString(cultureMsgKey, CultureInfo.CreateSpecificCulture("en-US"));

Though, I have created the culture in the parameter, it is advised to create a CultureInfo object instead, and pass it as a parameter. It will save you from the multiple creation of the same culture in case of accessing more than 1 key from the resource file.

Detail Info