A resource file consists of non-executable data that is
required by the application and is deployed along with it. Bitmaps, icons,
cursors, etc. are typical examples of resource files. Microsoft .NET provides
support for multilingual applications using resource files. The resource files
used to create a multilingual application contain the culture dependent
resources for the application. A resource file can be used to specify the
locale-specific settings. Note that to support each locale in the application,
there should be one resource file created. These resource files are then
compiled into satellite assemblies and accessed by the application. Resource
files can be either text files or resx files in .NET.
The basic advantages of using resource files are given
Support for Globalization with isolation of the resource content
from the application
Reusability and the provision for change of the resource content
without the need to change the application's code
It should be noted that each resource file has support for a
specific culture. Hence, if we need to have support for "n"
cultures, we need to have "n" different resource files.
Creating Resource Files
First create a resource file using the VS.NET IDE and save
using either a .txt or a .resx extension. The resource file should be created
using the intended culture if it is to be used for Globalization or
When creating resource files for specific locales, the
following naming convention should be followed.
<base file name>.<locale>.txt
<base file name>.<locale>.resx
Therefore the resource file targetted at en-GB locale should
be named as TestResource.en-GB.txt or TestResource.en-GB.resx. Note that the
resource file name TestResource.en-GB.resx contains the name of the resource
that it is intended at.
The ResourceWriter class in the System.Resources namespace
is used to create a resource programmatically. The sample code below creates a
resource file called Test.Resources in the root directory of the C drive.
public static void Main(stringargs)
ResourceWriter rw = new ResourceWriter("C:\\Test.resources");
rw.AddResource("CopyRight", "CopyRight Message in English");
The ResourceWriter class can also be used to store any other
serializable object in the resource file.
Reading Resource Files
The content of the resource files can be read in the
application using the ResourceManager class defined in the System.Resources
namespace. The ResourceManager class looks up culture-specific resources and
provides convenient access to culture-specific resources at runtime. According
to MSDN, "The Resource Manager class looks up culture-specific resources,
provides resource fallback when a localized resource does not exist, and supports
ResourceManager resourceManager = new
CultureInfo cultureInfo = new CultureInfo(culture);
string message = resourceManager.GetString("ID",cultureInfo);
Compiling Resource Files
Refer to the section above. Note that we had created a
resource file named TestResource.en-GB.resx. Now let us compile the resource
using the ResGen utility shipped with the Microsoft .NET Framework to create a
compiled resource that would have a .resources extension.
resgen Internationalization.en-GB.resx Internationalization.en-GB.resources
When used, the above command line tool would compile the
.resx file to a compiled binary .resources file for the en-GB locale.