|
Adding resources to an assembly
The C# Column - Yashawant Kanetkar
An
assembly can contain resource files. An assembly containing resources
is known as a satellite assembly. A resource file can contain any
resource such as text, string tables, pictures, etc. The resource
file can be embedded inside the assembly. In this article we plan
to create a resource file called quotes.resources in
an application called res and embed this file in another assembly
called resclient. The quotes.resources file would contain
five images and five quotes as resources. The assembly in which
we would embed this file will simply pick out a random image and
a quote and display them on a form. Let us first see how to build
a resource file.
Create a Console Application. Add the following
code in the Main( ) method.
using System;
using System.Resources;
using System.Drawing;
static void Main ( string[ ] args )
{
ResourceWriter rw = new ResourceWriter(quotes.resources);
Image img0 = Image.FromFile(heart.jpg);
Image img1 = Image.FromFile(fire.jpg);
Image img2 = Image.FromFile(face02.jpg);
Image img3 = Image.FromFile(face03.jpg);
Image img4 = Image.FromFile(face05.jpg);
rw.AddResource(img0, img0);
rw.AddResource(img1, img1);
rw.AddResource(img2, img2);
rw.AddResource(img3, img3);
rw.AddResource(img4, img4);
rw.AddResource(quo0, Health is wealth);
rw.AddResource(quo1, A long journey starts with a single
step);
rw.AddResource(quo2, Better late than never but better
never late);
rw.AddResource(quo3, Opportunity knocks for those who
listen);
rw.AddResource(quo4, The best time to make a friend is
before you need one);
rw.Close();
}
After creating a ResourceWriter reference
we have we created five Image references img0 through img4 and initialised
them with images from five different files. Next we added these
resources in the file using the AddResource( ) method of the ResourceWriter
class. The first argument of the method specifies the key and the
second argument specifies the image object to be added. The key
that we have added is in the form imgnumber. Note that the image
files must either be in the bin/debug folder or their
path must be specified.
Next we have added five quotes to the same
file with the key format quonumber. Then we have closed the ResourceWriter
by calling the Close( ) method.
On executing this application a file called
quotes.resources gets created. Now let us see how to
embed this file in another assembly. For this we have created a
simple windows forms application called resclient and added a PictureBox
named pic and a Label named quote to it. We have changed the font
size and colour of the text that would get displayed in the label
using the Properties window.
To embed the resource file we need to select
the Add | Add Existing Item from the context menu that
pops up on right-clicking the Solution Explorer. Select the quotes.resource
file. The file gets displayed in the Solution Explorer. In the Properties
window of this resource file change the BuildAction from None to
Embedded Resource so that the resource gets embedded into the output
assembly. The Properties window is shown in the following
figure.
After this much is done, we have added
code to the constructor of the form class to display a quote and
a picture in the form. The constructor is given below:
public Form1()
{
InitializeComponent();
Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager(resclient.quotes,
asm);
Random r = new Random();
string str;
int i = r.Next(4);
str = string.Format( img{0}, i);
pic.Image = (Image)rm.GetObject(str);
i = r.Next(4);
str = string.Format(quo{0}, i);
tip.Text = (string) rm.GetObject(str);
}
To use the embedded resource, we have used
the ResourceManager class defined in the System.Resources namespace.
We need to pass the reference of the Assembly class where the resources
are embedded to the constructor of the ResourceManager class. In
this case the file is embedded in the executing assembly, so we
have passed the result of Assembly.GetExecutingAssembly( ) as the
second argument. The first argument is the root name of the resource
file. The root name is made of the namespace followed by the name
of the resource file without extension. We have collected the reference
of this ResourceManager object in rm.
To extract a resource from the file we
have used the GetObject( ) method. We have passed a key to this
method. This method returns us the object associated with the key
specified. To create the key we have used the Random classs
Next( ) method that would return any number between 0 and 4 (0 &
4 inclusive). Next we have set the Image property of pic and Text
property of quote to the Image and string objects respectively returned
by the GetObject( ) method. The output of this application is shown
in the following figure.
Every time we execute this application
we get a different picture and quote on the form.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanet@nagpur.dot.net.in |
|