resx file
resource files
.NET development
programming
tutorial

Loop through all the resources in a .resx file

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A .resx file is an XML-backed resource file used by .NET for strings, images, and other static resources. If you want to inspect every resource entry programmatically, the usual API is ResXResourceReader, which gives you each key-value pair as a DictionaryEntry.

Read a .resx File with ResXResourceReader

The basic pattern is simple:

  1. open the .resx file
  2. enumerate the entries
  3. cast each entry to DictionaryEntry
  4. inspect the key and value

Example:

csharp
1using System;
2using System.Collections;
3using System.Resources;
4
5class Program
6{
7    static void Main()
8    {
9        using var reader = new ResXResourceReader("Strings.resx");
10
11        foreach (DictionaryEntry entry in reader)
12        {
13            Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
14        }
15    }
16}

This is the standard answer when you need to loop through every resource defined in a .resx file on disk.

Understand What the Enumerator Returns

Each item from ResXResourceReader is a key-value pair:

  • 'entry.Key is the resource name'
  • 'entry.Value is the resource object'

That value may be a string, byte[], image-related object, or another serializable type depending on what the file contains.

So it is usually safer to inspect the type before making assumptions.

csharp
1using var reader = new ResXResourceReader("Strings.resx");
2
3foreach (DictionaryEntry entry in reader)
4{
5    Console.WriteLine($"{entry.Key} -> {entry.Value?.GetType().Name}");
6}

This is especially useful when the file is not just localization strings.

Include Metadata When Needed

Some .resx files contain comments or metadata that you may want to inspect. ResXResourceReader can expose richer node information when configured to use ResXDataNode objects.

csharp
1using System;
2using System.Collections;
3using System.Resources;
4
5using var reader = new ResXResourceReader("Strings.resx")
6{
7    UseResXDataNodes = true
8};
9
10foreach (DictionaryEntry entry in reader)
11{
12    var node = (ResXDataNode)entry.Value;
13    Console.WriteLine($"Key: {entry.Key}, Comment: {node.Comment}");
14}

This approach is useful when you are building tooling around resource files rather than just reading the runtime values.

Runtime Resources Are a Different Case

If your goal is to enumerate compiled resources already embedded in an assembly, ResXResourceReader is not the right tool. That is for raw .resx files. For compiled .resources or assembly-based resource lookup, the APIs are different, such as ResourceManager or ResourceReader.

That distinction matters because many people mean one of two very different things:

  • iterate a source .resx file on disk
  • inspect resources embedded in a compiled assembly

The title question points to the first one.

Handle File Paths and Encoding Realistically

.resx files are XML, so file path and project layout issues matter more than resource APIs in many failures. If your code cannot find the file, the reader never gets a chance to work.

A safer path pattern for tools is often:

csharp
var path = Path.Combine(AppContext.BaseDirectory, "Strings.resx");
using var reader = new ResXResourceReader(path);

That avoids depending on the current working directory in ad hoc ways.

Common Pitfalls

  • Using ResXResourceReader when the real target is a compiled resource assembly rather than a .resx file.
  • Assuming every resource value is a string.
  • Forgetting that enumeration returns DictionaryEntry objects, not raw strings.
  • Ignoring ResXDataNode when comments or metadata matter.
  • Debugging the reader API when the actual problem is simply the wrong file path.

Summary

  • Use ResXResourceReader to loop through all entries in a .resx file.
  • Each resource is returned as a DictionaryEntry with a key and value.
  • Inspect value types instead of assuming all resources are strings.
  • Enable UseResXDataNodes when you need comments or metadata.
  • Use runtime resource APIs instead if the resources are already compiled into an assembly.

Course illustration
Course illustration

All Rights Reserved.