Working with String Resources In .NET

By Peter A. Bromberg, Ph.D.
To "Print This Page" Link

Peter Bromberg

The .NET platform provides a fairly extensive set of utilities and classes for creating and working with resource files. You can work with resources in XML format, including binary data such as images. Resources can be compiled into your assemblies, or they can be used as external files. A full discussion of all the features is beyond the scope of an article, and besides, the .NET Framework and Visual Studio.NET documentation cover all of it quite extensively. . But probably one of the most common uses for external resource files is to make localized string messages (such as for error codes) available. In this case, when an application is released in different languages, recompilation is unneccesary - we only need to provide the corrent resource file.

So this short article will have a narrow focus - how to create a string resource file, and how to use it. I've put a solution together with 2 C# projects in it that you can download at the bottom of this page. I figured we might have some fun here so I've put together some of the typical "Magic 8 Ball" phrases you undoubtedly have seen( and maybe a few that you haven't) , and we'll create a resource file from these. Then, in our "Test Harness" app, we'll simply generate a random number in the range, convert it to a string, and display that particular message from the "oracle". One of the things to remember in creating string resources is that you are limited to "name=value" pairs. However, there is nothing to stop you from taking the value portion and storing multiple items in it by using a delimter character such as the pipe ( | ) symbol. Then you can add a little "helper" function that would parse apart your message into its respective parts so that you could build, say, an XML status aggregate from them (of course, there's nothing to stop you from storing the entire xml fragment as a string either).



Our first project is a console app that simply creates our resources file:

using System;
using System.Resources;
using System.Drawing;
using System.IO;

class CreateResource
{
static void Main()
{
Console.WriteLine("Creating Resource file...");
//call createResource function
createResource();
Console.WriteLine("DONE!!\nHit enter to quit:");
Console.Read();
}

static void createResource()
{
//using ResourceWriter allows us to write to binary *.resources files
ResourceWriter rw =new ResourceWriter("MyMessages.resources");
// add our string resources with name, value
rw.AddResource("1", "Signs point to yes.");
rw.AddResource("2", "Yes.");
rw.AddResource("3", "Reply hazy, try again. ");
rw.AddResource("4", "Without a doubt. ");
rw.AddResource("5", "My sources say no. ");
rw.AddResource("6", "As I see it, yes. ");
rw.AddResource("7", "You may rely on it.");
rw.AddResource("8", "Concentrate and ask again. ");
rw.AddResource("9", "Outlook not so good. ");
rw.AddResource("10", "It is decidedly so. ");
rw.AddResource("11", "Better not tell you now.");
rw.AddResource("12", "Very doubtful. ");
rw.AddResource("13", "Yes - definitely. ");
rw.AddResource("14", "It is certain.");
rw.AddResource("15", "Cannot predict now.");
rw.AddResource("16", "Most likely.");
rw.AddResource("17", "Ask again later.");
rw.AddResource("18", "My reply is no.");
rw.AddResource("19", "Outlook good.");
rw.AddResource("20", "Don't count on it.");
rw.AddResource("21", "Ask again later");
rw.AddResource("22", "Ask more later");
rw.AddResource("23", "Reply meainingless, ask again later");
rw.AddResource("24", "Should not predict now");
rw.AddResource("25", "More information later");
rw.AddResource("26", "Out to Lunch");
rw.AddResource("27", "Closed on Sundays");
rw.AddResource("28", "No chance in Hell");
rw.AddResource("29", "Do you have capslock on?");
rw.AddResource("30", "Did you log on to the domain?");
rw.AddResource("31", "What mail server are you using?");
rw.AddResource("32", "Have you tried rebooting?");
rw.AddResource("33", "Can you ping that server?");
rw.AddResource("34", "Is it plugged in?");
rw.AddResource("35", "Is the network light on at all?");
rw.AddResource("36", "What's your port number?");
rw.AddResource("37", "Did you leave your PC unlocked?");
//generate the resource
rw.Generate();
//close the resource file
rw.Close();
}
}

 

You can see that all we need to do is instantiate the ResourceWriter class, use the AddResource method, then call Generate and Close when we are done. You can also use XML or text files and the RESGEN.EXE utility to generate these files.

Our "Tester App" needs to set a reference to the physical "MyMessages.resources" file generated by the first app, and then we can show our new talents:

private System.Resources.ResourceManager rm =
new System.Resources.ResourceManager("ResourceClient.MyMessages",
System.Reflection.Assembly.GetExecutingAssembly());
............
private void button1_Click(object sender, System.EventArgs e)
{
   System.Random iRand =new System.Random();
   string theNum = iRand.Next(0,36).ToString();
   textBox1.Text = rm.GetString(theNum);
}

The code shown above is just the most important stuff, the full code for the WinForm test app is included in the download. When you press the "CONSULT ORACLE" button, you should see something like the image below:

Congratulations. You've just created and used your first string resource file!


Download the code that accompanies this article


 

Peter Bromberg is an independent consultant specializing in distributed .NET solutions Inc. in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com