Default Resource Files in Assemblies

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;
 

ResourceManager rm = new ResourceManager("MyAssembly.SomeNamespce.Properties.Resources", Assembly.GetExecutingAssembly());

string

myString = 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
 
 
Advertisements

One thought on “Default Resource Files in Assemblies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s