How to Use C# to Read resx Files in Visual Studio

If you ever developed some iOS or Android apps, you should know that for localization, the strings should be stored in some resource files and dynamically loaded in apps. What about Windows applications?

When I tried to store strings in a resource file in Visual Studio, I met some problems and finally found out the solution. In this tutorial, I would like to share my experience about how to dynamically load strings from a resx file.

How to create a resx File?

Create a new project. Notice that there is a default resx file named Form1.resx, which is auto-generated by Visual Studio. I ever made a mistake that I created all strings in this resx file. What happened later? As I edited some elements on viewer designer, and saved the Form, the resx file was totally cleaned up. So you have to remember that do not put strings in the Form1.resx.

Right-click on the project root, and add a new resource file named strings.resx.

Open the properties of the resx file. Set the Build Action as Content. Set Copy to Output Directory as Copy if newer.

build action

As such, when you build the project, the resx file will be automatically copied to the output directory.

How to Read resx File?

string resxFile = @".\strings.resx"; // relative directory to the executable file
using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
{
   this.Text = resxSet.GetString("app_title");
}