FAtechs is a Web Site where you can find a lot of things related to tech and Education. Our Posts are mostly about Programming, Information, Technology,Software, Reviews & Everything You need to Know. You can also find Books and Novels here. Your appreciation and criticism will be welcomed.

How to make a Resource ( .resx ) File and Read Data From .resx File

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

{

classProgram

{

staticvoid Main(string[] args)

{

try

// using try and
catch so if any exception occur, it will show the error

{

ResourceWriter rw = newResourceWriter(@"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);

}

}

}

}

Now make .resx file of that resource file by again going to command prompt and writing "resgen faisal.resource faisal.resx"

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.