About BhashaIndia | Contribute | SiteMap | Register | Sign in to Windows Live ID
  Developers Patrons
Hindi Tamil Kannada Gujarati Marathi Telugu Bengali Malayalam Punjabi Konkani Oriya Sanskrit Nepali
Home > Developers > Tutorial > dotnet > Resource Files Welcome Guest!

Understanding the Resource Files

By Aparna Ravindran

A resource is a non-executable data utilized by the application and deployed along with the application. A resource can be used either in an application as a part of the user interface or to display some error messages. It can contain bitmaps, icons, cursors, screen labels, audio/video clips and so on. Storing data in resource files allows you to change the data without recompiling the entire application
The .NET framework provides immense support to create and use the resource files. There are three different ways to create a resource file. If your resource contains only string data, then you can use the text file format to create the resource file. If your resource contains objects or combination of objects, then you must create the file using .resx or .resources file formats.
Creating Resources in Text File Format
Text files (.txt) can contain only string resources. The encoding formats UTF-8 or UTF-16 should be used to save the text files. In addition to string entries, comments can also be included as part of the file. All comment entries should begin with a semi-colon (;) or a hash (#) symbol to suppress it from being accessed as a resource.
All the resources in the text file are created as name-value pairs. It should be specified in the format as shown below:
name = value
Here, name is the string that describes the resource and value is the resource string that will be returned while reading the resource. The value cannot contain any new line characters. You can use “\n” and “\t” escape sequences to insert a new line and a tab space respectively.
An example for creating a resource text file is shown below:
            ; This is a sample resource application.
            ; It contains images.
            Description_1=Hello
            Description_2=Welcome
            Description_3=Hi
            Description_4=How are you?
Save the file as Description.txt.
In the above file, the first two lines are comment lines followed by four name-value pair items. Comments help you specify how the resources are organized. You can organize resources in a file by labeling them with comments such as ;images, ;exceptions, ;audio files and so on.
Creating Resources in .resx Format
The .resx format of creating a resource file specifies the resource information in the form of XML entries. The objects and string resources will be contained inside the XML tags.
A .resx file contains a set of header information. It includes the version number of the XML to parse the data and the encoding format to store the resources. The header information is followed by the specification of resources in name-value pairs. The name-value pair is wrapped into the XML tags describing the string or object values. When you add a string value to the .resx file, the name of the resource will be added, as an attribute, to the <data> tag and the value of the resource will be added to <value> the tag, which is a sub-tag of the <data> tag as shown below:
        <data name=”description_1”>
                     <value> Hello</value>
       </data>
You can also create the .resx file programmatically using the ResxResourceWriter class of the System.Resources namespace. This class contains the methods:
AddResource: Allows you to add the resource to the file.
Close: Allows you to write the resource information and to close the ResxResourceWriter class.
The following code snippet illustrates how to use the ResxResourceWriter class to create .resx file.
             Image im = Image.FromFile("C:\\sample2.jpg");
             ResXResourceWriter write = new ResXResourceWriter("sample.resx");
             write.AddResource("sample2.jpg", im)    
             write.Close();
In the above code, the “sample.resx” file will be created in application’s bin/debug directory.
To read the resource (image) stored in the .resx file, you use the ResxResourceReader class.
The following code illustrates how to use the class to read the resource (image) from the file:
             ResXResourceReader read = new ResXResourceReader("sample.resx");
             IDictionaryEnumerator id = read.GetEnumerator();
             foreach (DictionaryEntry de in read)
             {
             label1.Text = de.Key.ToString() + ":" + de.Value.ToString();
             pictureBox1.Image =   (System.Drawing.Image)de.Value;
             }
In the above code, the label1 displays the name of the image (sample. jpg) and the value of the image, which will be displayed as System.Drawing.Image. To display the actual image, you type cast the value property to the System.Drawing.Image class and display it in the pictureBox control.
Creating Resources in .resources Format
You can create .resources format of resource files in two methods. One method is to use the ResourceWriter class defined in the System.Resources namespace and the second method is to run the Resgen.exe utility. Resource files created in .txt format and .resx format can neither be embedded into a runtime executable nor compiled into a satellite assembly.
Note: Satellite Assemblies are compiled DLL’s that contain only resource data. These assemblies are used to load the resources dynamically.
The ResourceWriter class provides methods, namely AddResource and Close to create the .resources file. The working of these methods is similar to the AddResource method and the Close method defined in the ResxResourceWriter class. You can read the resource information using the ResourceReader class.
The Resgen.exe utility (Resource Generator File) converts .txt files and .resx files into the CLR binary .resources files that can be embedded into a runtime executable or compiled into a satellite assembly. Resgen.exe utility converts: 
  • The .txt files to .resource or to .resx file.
  • The .resx file to .txt or .resources file.
  • The .resources file to .txt or .resx file.
If the .resx file or the .resources file contains any embedded objects, then converting these files to the text format will result in the loss of these objects. The utility will report the error during such conversions.
The following code listing illustrates how to write a resource to a file and to read a resource from the file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
namespace resource
{
    class Form1 : Form
    {
           public Form1()
           {
            InitializeComponent();
        }
 // Writes a picture into the sample.resx  resource file.
 private void button1_Click(object sender, EventArgs e)
        {
            Image im = Image.FromFile("C:\\ sample2.jpg");
            ResXResourceWriter write = new ResXResourceWriter("sample.resx");
            write.AddResource("sample2.jpg", im);
            write.Close();
          }
   // Displays the picture read from the   sample.resx file
  private void button2_Click(object sender, EventArgs e)
        {
               label1.Text = "";
       ResXResourceReader read = new ResXResourceReader  
      
("D:\\resource\\bin\\Debug\\sample.resx");
       IDictionaryEnumerator id = read.GetEnumerator();
              foreach (DictionaryEntry de in read)
              {
                 label1.Text += " Name:  " + de.Key.ToString() +
                 ":::::" +"Value: "+de.Value.ToString()+"  ";
                 pictureBox1.Image = (System.Drawing.Image)de.Value;
               }
        }
  //Writes a text data into the Flowers.resources file
 private void button3_Click(object sender, EventArgs e)
        {
                      ResourceWriter write = new
                      ResourceWriter("Flowers.resources");
                      write.AddResource("F1", "Jasmine");
                      write.AddResource("F2", "Lilly");
                      write.AddResource("F3", "Sun Flower");
                      write.Close();
             }
            // displays the text data from the Flowers.resources file
            private void button4_Click(object sender, EventArgs e)
    {
                 label1.Text = "";
                 ResourceReader read = new ResourceReader("Flowers.resources");
                 IDictionaryEnumerator id = read.GetEnumerator();
                 foreach (DictionaryEntry de in read)
                {
                 listResource.Items.Add(de.Key.ToString() + "   " + de.Value.ToString());
                }
            read.Close();
        }
      }
}
The following figure displays the design view of the project
In the above figure, Write Image button is used to write an image into the resource file named sample.resx. The Read Image button is used to read an image from the sample.resx file and displays the image in a picturebox control. This file is created during runtime. The label control displays the key and value information of that image when reading the image.
The Write Data button is used to write a text data into the resource file named Flowers.resources. The Read Data button is used to read a text data from the Flowers.resources file. The contents of the file are displayed in a list box control named listResource.
The following figure shows the output of executing the above form:
The following figure displays the image when Read Image button is clicked.
The following figure displays the text data when Read Data button is clicked.
Therefore, when you create and store data in the resource files, you can use these files without re-compiling them, if any modification is made to those files. You can use these files to load the resources dynamically to an application. These resource files are also used to store culture-specific information for each locale.

Partner Profile | Privacy Statement | Why Passport | Testimonials
This site uses Unicode for non-English characters and uses Open Type fonts.
©2003-2007 Microsoft Corporation. All rights reserved.