Introduction

This article explains how to read a RESX file in our application using C#. I hope you will like it.

Please see this article in my blog here: Read RESX File .

Background

A few days ago I used a RESX file in my application and I set some error codes and details in it. It worked like a charm. So I thought of sharing with you a demo of how to use a RESX file in our application.

Download Source Code

RESX files

  • 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 have created the application, you need to create a RESX file by clicking the “New Item” menu item.


New Item
Figure 1: Add New Item


Resource file
Figure 2:
Resource file

Now you can see a new file in your Solution Explorer named Resource1.resx.

Resource1.resx
Figure 3: Resource1.resx

So our RESX file is ready, right? Now we can set some values in it. You can see the values I set in my RESX file in the preceding image.

RESX file
Figure 4: RESX file

Now add a web page and go to the code behind by right-clicking then selecting view code . What you need to do next is, add the necessary namespaces. You can determine them from the preceding code block.

  1. using System.Reflection;  
  2. using System.Resources;  
  3. using System.Globalization;  

Once it is done, you are ready to go. Please paste the following code in page load:

  1. ResourceManager rm = new ResourceManager("UsingRESX.Resource1",  
  2. Assembly.GetExecutingAssembly());  
  3. String strWebsite = rm.GetString("Website",CultureInfo.CurrentCulture);  
  4. String strName = rm.GetString("Name");  
  5. form1.InnerText = "Website: " + strWebsite + "--Name: " + strName;  

So in the preceding code block we are getting the values of “Name” and “Website” that we have already set in the RESX file.

  1. ResourceManager rm = new ResourceManager("UsingRESX.Resource1",  
  2. Assembly.GetExecutingAssembly());  

In the preceding code block we are setting our Resource file to the ResourceManager class. Please note 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.

Output
Figure 5: 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.

Next Recommended Readings