.NET Resources

Maybe this is clear to everyone else, but as I’ve been working on some internationalization problems for the .NET application I’m writing, and I’ve been having a tough time finding clear tutorials on the web on how to work with .NET resouces. As part of my give back to the Net initiative, I’m going to give a quick tutorial myself from what I’ve learned, in the hope that it will save other people some time.

First, some background on my development environment. I’m using .NET 1.1 in Visual Studio 2003. I’m not sure if this information is applicable to VS 2005.

The first thing to understand is that there are two type of resource files in .NET: .resx files and .resource files. .resx files are XML files that are designed to be user editable (with the help of tools). .resx files are compiled to .resource files, which are binary files designed to provide needed performance when accessing resources at runtime.

.resx files can contain more than text. In fact, binary data such as images can actually stored in them, it encoded in base64 to be stored in the text format. Every project that uses Windows Forms has many .resx files. They are created for every Windows Form automatically (named XYZ.resx for ever Windows form stored in XYZ.cs in C#). The .resx files for Windows forms are not shown in the project in the Solution Explorer, so unless you examine the file system manually, you won’t see them. This can be somewhat confusing, because when you add resources manually, they do show up in the Solution Explorer.

Ok, let’s do a quick walk through of how to add a resource to your project. First we need to add the .resx file to the project. My preferred way to do this is to right-click on the project in the Solution Explorer and chose Add –> Add new item.. From there add an Assembly Resource File in the Resources folder. Name it whatever you want, for the purpose of discussion, I’ll pretend it’s called FooResources.resx. After you’ve added the resource file to the project, you can edit the file by double-clicking on the resx file in the Solution Explorer. Enter the name by which you will access this resource. Also enter the value.

the resourc editor

Save the file. Now from your code, you can access the resource by first creating a System.Resources.ResourceManager and then accessing the resource by name. The following code will do just that.

System.Resources.ResourceManager manager
    = new System.Resources.ResourceManager( "DummyApp.FooResources",
                    System.Reflection.Assembly.GetExecutingAssembly() );
System.Console.WriteLine( manager.GetString( "Greeting" ) );

The confusing part of this code is where the DummyApp comes from. When Visual Studio compiles the .resx file to a .resources file, it renames it according to the default namespace for the project.

project properties

For this project, the default namespace is DummyApp. When the project is compiled, the file DummyApp.FooResources.resources is created in the obj\Debug directory. The resources will be compiled into the project’s assembly, and the second parameter to the ResourceManager constructor retrieves this assmbly at runtime. If you would like to download the source code to this demo application, it is available here.

As mentioned previously, it is also possible to add things other than strings to resource files. To do this it is best to use an external tool, as the built in editor provided in Visual Studio is ill-equipt.  Lutz Roeder has a bunch of good .NET tools including his resource editor.  The code project also has some good ones, such as Gaston’s and Bill Sayles’.

Finally, it is important to understand that I have left out a good deal of information about the various ways resources can be included in the application.  Resources can be embedded in the assembly or distributed separately.  For information about this please refer to other documentation.

Leave a Reply