C#
.resx files
resource management
file handling
localization

Read string from .resx file in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, you usually read strings from a .resx file through generated resource classes rather than parsing the XML yourself. That gives you strongly named properties, culture-aware lookups, and cleaner code. Direct .resx parsing is possible, but it is mostly reserved for tooling and diagnostics.

The Usual Approach: Generated Resources Class

When a .resx file is added to a .NET project, Visual Studio or the build system typically generates a class for it. If the file is named Resources.resx, you can access entries as properties on Resources.

Suppose the resource file contains a key named WelcomeMessage with the value Hello from resources.

csharp
1using System;
2
3namespace ResourceDemo;
4
5internal static class Program
6{
7    private static void Main()
8    {
9        string message = Properties.Resources.WelcomeMessage;
10        Console.WriteLine(message);
11    }
12}

This is the normal application-level solution because it is simple and type-safe. You do not need to know where the resource file lives on disk once it is compiled into the assembly.

Reading by Key with ResourceManager

If you want to fetch a string dynamically by name, use the ResourceManager behind the generated class.

csharp
1using System;
2using System.Globalization;
3
4namespace ResourceDemo;
5
6internal static class Program
7{
8    private static void Main()
9    {
10        string? message = Properties.Resources.ResourceManager.GetString(
11            "WelcomeMessage",
12            CultureInfo.CurrentUICulture);
13
14        Console.WriteLine(message ?? "Resource not found");
15    }
16}

This is useful when resource keys are not known at compile time, such as plug-in systems, dynamic forms, or shared lookup utilities.

Localized .resx Files

Resource lookup becomes more powerful when you add culture-specific files such as Resources.fr.resx or Resources.de.resx. .NET automatically selects the best match based on CurrentUICulture.

csharp
1using System;
2using System.Globalization;
3using System.Threading;
4
5Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
6
7Console.WriteLine(Properties.Resources.WelcomeMessage);

If a French resource exists for that key, the French string is returned. Otherwise, .NET falls back through its resource resolution chain until it finds a matching entry, usually ending at the default Resources.resx.

Reading a Standalone .resx File Directly

If you are building a tool that inspects .resx files before compilation, use ResXResourceReader. This reads the XML file directly from disk.

csharp
1using System;
2using System.Collections;
3using System.Resources;
4
5var reader = new ResXResourceReader("Resources.resx");
6
7foreach (DictionaryEntry entry in reader)
8{
9    if ((string)entry.Key == "WelcomeMessage")
10    {
11        Console.WriteLine((string)entry.Value);
12    }
13}

This is not the normal runtime pattern for desktop or web applications. It is more appropriate for editors, migration utilities, or custom build tools.

Why Not Parse the XML Manually?

A .resx file is XML, but treating it as generic XML usually creates more work. You would need to understand resource schema details, special handling for encoded values, and the conventions .NET already implements for you.

Use the generated Resources class for application code. Use ResourceManager for dynamic access. Use ResXResourceReader only when you truly need the raw file.

Common Pitfalls

One common issue is editing the .resx file correctly but not seeing the generated property in code. Usually that means the custom tool did not regenerate the designer file, or the file's access modifier is not configured as expected.

Another problem is confusing CurrentCulture with CurrentUICulture. Resource lookup uses CurrentUICulture, not the formatting culture used for dates or numbers.

Developers also sometimes hard-code resource keys all over the codebase. That works, but it removes one of the biggest benefits of .resx files: strongly named access through generated properties.

Finally, direct file access with ResXResourceReader requires the physical .resx file to exist where your code expects it. Compiled applications often ship satellite assemblies instead, so a path-based lookup may fail even though resource access through ResourceManager works perfectly.

Summary

  • Use the generated Properties.Resources class for the simplest and safest string access.
  • Use ResourceManager.GetString when the resource key is dynamic.
  • Use localized .resx files with CurrentUICulture for multi-language applications.
  • Use ResXResourceReader only when you need to inspect a raw .resx file directly.
  • Avoid manual XML parsing unless you are building tooling that truly needs the file format itself.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.