Resource ( .Resx ) File :
A
resource file is a collection of resources like images and audio files. Resources
are stored in key-value pairs. The
Key identifies the resource by name, and the value refers to the resource, as
in a hash table.
You can add up to 4 GB of data in Resource file.
How to make a resource file:
The Resgen.exe utility comes along with .Net Framework SDK. This utility is used to create resource files. Steps are given below to make a resource file.
- Make an empty .txt file on Notepad for example faisal.txt, save it in any folder and copy the address of that folder.
- Open Visual Studio Command Prompt, write command "cd [path of folder]" and press enter to go on that path.
- Then write "resgen faisal.txt" to make a resource file. Do not close Command Prompt.
- A resource file will be made on the folder where you save .txt file, mine made on desktop
- Now write resources on it using C# Application, by using Console Application with following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Drawing;
namespace lab4_rosource_reader
{
class Program
{
static void Main(string[] args)
{
try // using try and
catch so if any exception occur, it will show the error
{
ResourceWriter rw = new ResourceWriter(@"C:\Users\Faisal\Desktop\faisal.resources"); // it is the name of text
file which exists on the desktop
Image img;
img = Image.FromFile(@"1.jpg"); // name of a jpg image exist on debug folder of program so
we have not to enter the path like above
rw.AddResource("1Image",
img);
rw.AddResource("1Name", "Faisal Jabbar");
//name
rw.AddResource("1Phone", "0092333*******");
// Phone Number
img = Image.FromFile(@"2.jpg");
rw.AddResource("2Image", img);
rw.AddResource("2Name", "Abdul Jabbar");
rw.AddResource("2Phone", "00920333*******");
img = Image.FromFile(@"3.jpg");
rw.AddResource("3Image", img);
rw.AddResource("3Name", "Aqib");
rw.AddResource("3Phone", "00920333*******");
img = Image.FromFile(@"4.jpg");
rw.AddResource("4Image", img);
rw.AddResource("4Name", "Zohaib");
rw.AddResource("4Phone", "00920333*******");
img = Image.FromFile(@"5.jpg");
rw.AddResource("5Image", img);
rw.AddResource("5Name", "tabish");
rw.AddResource("5Phone", "00920333*******");
img = Image.FromFile(@"6.jpg");
rw.AddResource("6Image", img);
rw.AddResource("6Name", "Junaid");
rw.AddResource("6Phone", "00920333*******");
img = Image.FromFile(@"6.jpg");
rw.AddResource("7Image", img);
rw.AddResource("7Name", "Saqib");
rw.AddResource("7Phone", "0333*******");
Console.WriteLine("Resources
written over resource file");
rw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
- A file will be made on desktop or wherever your .resources file is name "faisal.resx".
- Attach that file to any Application by going to Solution Explorer and add existing item and use that resource.
- Code is given below:
private void btn_data_Click(object sender, EventArgs e)
{
try
{
assembly = Assembly.GetExecutingAssembly();
rm = new ResourceManager("lab5_resourse_writer.faisal", assembly); // resx file that is attached using solution explorer
for (int Count = 1; Count <= 7; Count++) // there are data of 7 people
{
this.img_Resources.Images.Add((Image)rm.GetObject(Count + "Image"));
this.listBox1.Items.Add(rm.GetString(Count
+ "Name"));
this.Phone_no.Add(rm.GetString(Count
+ "Phone"));
}
pictureBox1.Image = this.img_Resources.Images[0];
this.listBox1.SelectedIndex = 0;
this.btn_next.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.txt_phone.Text = (string)this.Phone_no[this.listBox1.SelectedIndex];
this.pictureBox1.Image = this.img_Resources.Images[this.listBox1.SelectedIndex];
}
Thank You for reading. Like this if you understood, It helps a lot to keep us motivated.
0 comments:
Post a Comment