How do I access string resources in the default resource file for an assembly?
Accessing strings in a resource file for a web application is fairly straight forward using GetLocalResourceObject and GetGlobalResourceObject. Since the web application that I am currently writing uses additional assemblies I needed a way to localise some of the string values stored in these files. Obviously, I can add individual resource files to this separate assembly and access then using the ResourceManager. One of the articles I read about this included this one http://www.codeproject.com/KB/cs/cs-multi-assembly-res.aspx.
My issue was that I didn’t know how to access the content when adding a default resource file. The default resource file is generated when you view the Properties of an assembly and select the Resources tab. If you have not already created a default resource fle you will see the message This project does not contain a default resource file. Click here to create one. Once you have selected this option and created the default file, the question is how to I get to the content stored in it?
The answer is from the resource manager but ensuring you specify the full baseName parameter for the ResourceManager constructor. For example, if I have an assembly with an Assembly Name of MyAssembly.SomeNamespace and have created a default resource file called Resources.resx then I can access a string resource in the default resource file using the following code;
ResourceManagerrm = new ResourceManager("MyAssembly.SomeNamespce.Properties.Resources", Assembly.GetExecutingAssembly());
stringmyString = rm.GetString("MyStringResource");
This is because the default resource file resides under the Properties folder of the satellite assembly. Details about the resource manager can be found here http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx