How to read RESX file in C#
[toc]
Introduction
In this article, you will learn how we can read a RESX file in our application using c#. I hope you will like it.
Download Source Code
Please feel free to download the source code from here: Using RESX
Background
A few days back I used a RESX file in my application and I have set some error codes and details in it. It worked like a charm. So I thought of sharing you a demo of how we can use RESX file in our application.
What is RESX file?
- The .resx resource file format consists of XML entries
- These XML entries specify objects and strings inside XML tags
- It can be easily manipulated
Using the code
To start with, you need to create a web application. Here I am using Visual Studio 2012 and C# as the language.
Once you created the application, you need to create a RESX file by clicking the “New Item”
Now you can see a new file in your solution explorer named Resource1.resx
So our RESX file is ready, right? Now we can set some values to that 🙂 You can see the values I set to my RESX file in the preceding image.
Now add a web page and go to the code behind by right click+view code . What you need to do next is, adding needed namespaces. You can find out them from the preceding code block.
[csharp]
using System.Reflection;
using System.Resources;
using System.Globalization;
[/csharp]
Once it is done, you are ready to go. Please paste the below mentioned code in your page load.
[csharp]
ResourceManager rm = new ResourceManager(“UsingRESX.Resource1”,
Assembly.GetExecutingAssembly());
String strWebsite = rm.GetString(“Website”,CultureInfo.CurrentCulture);
String strName = rm.GetString(“Name”);
form1.InnerText = “Website: ” + strWebsite + “–Name: ” + strName;
[/csharp]
So in the above code block we are getting the values of “Name” and “Website” which we have already set in the RESX file. Cool right?
[csharp]
ResourceManager rm = new ResourceManager(“UsingRESX.Resource1”,
Assembly.GetExecutingAssembly());
[/csharp]
In the above mentioned code block we are setting our Resource file to the ResourceManager class. Please be noted that in UsingRESX.Resource1, UsingRESX is my project base name and Resource1 is my resource file name. The function GetString is used to read the resource file properties 🙂
Output
Now it is time to see the output.
Conclusion
Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
Kindest Regards
Sibeesh Venu