Partager via


Resources in .Resources File Format 

The ResourceWriter class is specifically designed to create .resources files. Remember that you can save objects only in .resources and .resx resource files. Only resource files in the .resources file format should be embedded in a runtime executable or compiled into a satellite assembly. You can either use the ResourceWriter class directly from code or use the Resource File Generator (Resgen.exe) to create .resources files.

NoteNote

Do not use resource files to store passwords, security-sensitive information, or private data.

Using the ResourceWriter Class

You can use the ResourceWriter class to create a .resources file directly from code. First, create a ResourceWriter with a unique file name. Next, call the ResourceWriter.AddResource method for each string to add to the file. Finally, call the ResourceWriter.Close method to write the strings to the resource file and close the ResourceWriter. The following example illustrates this process.

Imports System
Imports System.Resources

Public Class SampleClass

    Public Shared Sub Main()
        ' Create a resource writer.
        Dim rw As IResourceWriter
        rw = new ResourceWriter("myStrings.resources")
        ' Add resources to the file.
        rw.AddResource("color1", "red")
        rw.AddResource("color2", "green")
        rw.AddResource("color3", "blue")
        ' Close the ResourceWriter.
        rw.Close()
    End Sub
End Class
using System;
using System.Resources;

public class SampleClass
{
    public static void Main()
    {
        // Create a resource writer.
        IResourceWriter rw = new ResourceWriter("myStrings.resources");
        // Add resources to the file.
        rw.AddResource("color1", "red");
        rw.AddResource("color2", "green");
        rw.AddResource("color3", "blue");
        // Close the ResourceWriter.
        rw.Close();
    }
}

Using Resgen.exe

The Resource File Generator (Resgen.exe) converts .txt files into .resources files by wrapping the methods implemented by the ResourceWriter class. Resgen.exe also wraps a ResourceReader, which allows you to use the tool to convert .resources files back into .txt files.

NoteNote

When Resgen.exe reads a text file, the comments are lost and will not be written to the resulting .resources or .resx file.

If the text file contains duplicate resource names, Resgen.exe will emit a warning and ignore the duplicate names.

The following Resgen.exe command creates the resource file strings.resources from the input file strings.txt.

resgen strings.txt

If you want the name of the output file to be different from the input file, you must explicitly specify the name of the output file. The following command creates the resource file MyApp.resources from the input file strings.txt.

resgen strings.txt MyApp.resources

The following command creates a text file strings.txt from the input file strings.resources. Note that you should perform this type of conversion only on a .resources file that contains only strings. Any object references cannot be written to the .txt file.

resgen strings.resources strings.txt

Resgen.exe converts .resx files into .resources files by wrapping the methods implemented by the ResourceWriter class. Resgen.exe also wraps a ResourceReader, which allows you to use the tool to convert .resources files back into .resx files.

The following Resgen.exe command creates the resource file items.resources from the input file items.resx.

resgen items.resx

The following command creates the .resx file items.resx from the input file items.resources. Note that in converting a .resx file to a .resources file all objects are preserved.

resgen items.resources items.resx
NoteNote

If Resgen.exe fails for any reason, the return value will be –1.

See Also

Reference

Resource File Generator (Resgen.exe)

Concepts

Creating Resource Files
Resources in Text File Format
Resources in .Resx File Format