What is .Resx file in Dot Net?

What is .Resx file in Dot Net? How to access?

Resource files are used in .NET to store culture-specific data all in one place, separate from the code. For example, suppose you are developing a multi-language Web site and you have a form on a page with a label beside a text field and the label in English says "First Name." Instead of having code like this:

if (language == "English")
{
lblFirstName = "First Name";
}
else if (language == "German")
{
lblFirstName = "Vorname";
}
You can just do this:

ResourceManager resourceManager = new ResourceManager("nameSpace.resourceFileBaseName",
Assembly.GetExecutingAssembly());
lblFirstName = resourceManager.GetString("lblFirstName");
And that's it—just two lines. There's no need to change a million sections of code each time you add or take away a language.


Reading from .Resx File:

You can access the .Resx file using this code:

ResXResourceReader reader = new ResXResourceReader(Server.MapPath("fileName.resx"));
IDictionaryEnumerator rsxr = reader.GetEnumerator();
foreach (DictionaryEntry d in reader)
{
Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
}
//Close the reader.
reader.Close();

Writing to .Resx File:

We can write the binary data of an image to the .Resx file using the following code

Image img = Image.FromFile("abc.jpg");
ResXResourceWriter rsxw = new ResXResourceWriter("abc.resx");
rsxw.AddResource("urdu.jpg",img);
rsxw.Close();

Use of .Resx File:

It is very useful while working on Localized project [Multiple Languages], You can make different resource files for different languages and depending upon the user choice you can change the Language of the application.

0 comments:

Post a Comment

Thank you very much