Using String Resources in Delphi.Net

The first thing that became apparent was that a lot of unsafe code was being used to access string resources. Turbopower also used a proprietary string resource compiler to create the .RES files from text files.

A little research revealed much better ways of doing this in .Net. By using the ResGen tool and the ResourceManager component supplied with the .Net 2.0 SDK it was possible to eliminate the unsafe code and drastically reduce the size and complexity of the string resource handling code as well.

To summarize, you need to take the following steps to create and use string resources in a Delphi.Net application:

  1. Create a text file containing the strings that are to be included in the applications resources.
  2. Compile the text to a resource file using ResGen.
  3. Add the compiled resource to your Delphi project by including a $R directive in one of your source modules.
  4. Use the Microsoft ResourceManager class to retrieve the resource strings as needed.

Creating the Text File

You can use any text editor to create the text file which defines your string resources. The strings are specified as name=value pairs, one string per line.  The file must be saved with a .TXT extension.  Your text file will look something line this:


Creating the Resource File

Once you have created your text file you need to compile it into a resource file.  You do this using the ResGen tool that is supplied with the .Net 2.0 SDK.  I did this by adding a Build Event to my project’s options.  To do this right click the project name in the project manager and select Options | Build Events.  Click the Edit button next to the Commands edit box in the Pre-build section.  Add the following command for each resource file that you need to create:

resgen strings.txt strings.resources

where strings is the name of your resource.  You will use this name to access to strings resources from your application.

Note: You will probably have to add the .Net 2.0 SDK bin directory to your path to allow the build process to find resgen.

Accessing the Strings in Your Application

To include the string resources into your Delphi.Net application you need to add a $R directive to one of your source modules.  It will look like this:

{$R strings.resources}

To access the string resources you use the ResourceManager class.  This is fairly well documented in the .Net manuals, but an example is worth many pages of documentation.


The following code snippet will extract the values of the string resources string1, string2 and string3 from the strings.resources resource file and display them in a TListBox.

{$R strings.resources}

procedure TTestForm.GetResBtnClick(Sender: TObject);
NAMES: array [1..3] of String = ( ’string1′, ’string2′, ’string3′ );
rm: ResourceManager;
i: Integer;
rm := ResourceManager.Create(’strings’,
for i:=1 to High(NAMES) do

Leave a Reply