|
The coming section will discuss about some more classes used for Globalization in C#.
Resources and ResourceWriter
Resources such as pictures or string tables can be put into resource files or satellite assemblies. Such resources can be very helpful when localizing applications, and .NET has built-in support to search for localized resources. A resource file can be a text file or a .resx file or XML file. This file contains the key/value pairs. Where the keys are used in the program and at run time they are replaced with the values that is corresponding to each key. For example:-
Name = Jim Tire
Address = USA
Author = Puppet
Publisher = Jim Toe
Note:-ResGen.exe utility is used to create a resource file out of this “test.txt” file. the command Resgen test.txt will create the text.txt resource file and Resgen test.txt test.resx will createtthe XML file from the test.txt file.
ResourceWriter
This class under the System.Resources namespace is used to create the resource file through the programming. The below example will create the object of the ResourceWriterAddResource() method. and then we can use the same object to add upto 2GB of resources. We have to just supply the key/value pair to the object's
Example: Demonstrate ResourceWriter
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Threading;
using System.Globalization;
using System.Resources;
namespace ConsoleApplication1
{
class Program1
{
static void Main ( string [] args)
{
ResourceWriter rw = new ResourceWriter ( "RWDemo.resources" );
rw.AddResource( "Name " , "VisualBuilder" );
rw.AddResource( "Adress" , " UK " );
rw.AddResource( "Hobby" , "Cricket" );
rw.AddResource( "Favourite Country" , " Australia " );
rw.AddResource( "Player" , "Sunny" );
rw.Close();
}
}
}
Output:
This will create RWDemo.resources file, which contains the key/value pairs. The Close() method of the ResourceWriter class automatically calls ResourceWriter.Generate() to finally write the resources to the file RWDemo.resources.
Using Resource File in an Application
We can add resource files to assemblies using the assembly generation tool al.exe, using the /embed option or using the c# compiler with /resource option or directly using the VS.Net. The following are the steps to use the resource files in C# application.
- Open the application
- Use the context menu of the solution explorer (Add >> Add Existing Item)
- Locate the path where the resource file is situated.
- If you check the Resource after adding it to the projects, its BuildAction is “Embedded Resource”, this means the resource is embedded to the output assembly.
- You can view the resource file using the ILDasm.exe utility by checking it in the assembly manifest.
- To access the embedded resources, use the Resource Manager class from the System.Resources namespace.
- You can pass the assembly that has the resources as an argument to the constructor of the Resource Manager class
- Once you include the resource file in the application use the Resource Manager and load the resource file into this.
Example: Demonstrate Usage of Resource File
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;
namespace cSHARPEXAMPLES
{
public partial class Form22 : Form
{
public Form22()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
System.Resources. ResourceManager rm;
Assembly assembly = Assembly .GetExecutingAssembly();
rm = new ResourceManager ( "cSHARPEXAMPLES.RWDemo" , assembly);
txtName.Text = rm.GetString( "Name " );
txtAddress.Text=rm.GetString( "Adress" );
txtHobby.Text = rm.GetString( "Hobby" );
txtFCountry.Text = rm.GetString( "Favourite Country" );
txtPlayer.Text = rm.GetString( "Player" );
}
}
}
|