다음을 통해 공유


Resources in .Resx File Format 

The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags. One advantage of a .resx file is that when opened with a text editor (such as Notepad or Microsoft Word) it can be written to, parsed, and manipulated. When viewing a .resx file, you can actually see the binary form of an embedded object (a picture for example) when this binary information is a part of the resource manifest. Apart from this binary information, a .resx file is completely readable and maintainable.

NoteNote

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

A .resx file contains a standard set of header information, which describes the format of the resource entries and specifies the versioning information for the XML used to parse the data. The following example shows what a typical set of header statements in a .resx file might look like.

<?xml version="1.0" encoding="utf-8"?>
<root>
    <xsd:schema id="root"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="data">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="value" type="xsd:string" minOccurs="0"
                    msdata:Ordinal="2" />
                </xsd:sequence>
                    <xsd:attribute name="name" type="xsd:string" />
                    <xsd:attribute name="type" type="xsd:string" />
                    <xsd:attribute name="mimetype" type="xsd:string" />
            </xsd:complexType>
        </xsd:element>

Following the header information, each entry is described as a name/value pair, very similar to the way in which strings are specified in a .txt file. A name/value pair in the .resx format is wrapped in XML code, which describes string or object values. When a string is added to a .resx file, the name of the string is embedded in a <data> tag, and the value is enclosed in a <value> tag, as in the following example.

    <data name="string1">
        <value>hello</value>
    </data>

When an object is inserted into a .resx file, the same <data> and <value> tags are used to describe the entry, but the <data> tag includes either a type or MIME type specifier. The type specifier holds the data type of the object being saved. The MIME type specifier holds the base type (Base64) of the binary information stored, if the object consists of binary data.

NoteNote

All .resx files use a binary serialization formatter to generate and parse the binary data for a specified type. As a result, a .resx file can become invalid if the binary serialization format for an object changes in an incompatible way.

The following example shows an Int32 object saved in a .resx file, and the beginning of a bitmap object, which holds the binary information from an actual .gif file.

    <data name="i1" type="System.Int32, mscorlib">
        <value>20</value>
    </data>

    <data name="flag" type="System.Drawing.Bitmap, System.Drawing,   
    Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
    mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>
            AAEAAAD/////AQAAAAAAAAAMAgAAADtTeX…
        </value>
    </data>

Using the ResXResourceWriter Class

You can use the ResXResourceWriter class to create a .resx file directly from code. The following example illustrates how to create a .resx file that stores a .jpg file as one of the resources inside the file. First, create the image using the Image.FromFile method. Next, create a ResXResourceWriter with a unique file name. Call the ResXResourceWriter.AddResource method for each image to add to the file. Finally, call the ResXResourceWriter.Close method to write the image information to the resource file and close the ResXResourceWriter.

Imports System
Imports System.Drawing
Imports System.Resources

Public Class SampleClass

    Public Sub Main()
        Dim img As Image
        Dim rsxw As ResXResourceWriter
        img = Image.FromFile("en-AU.jpg")
        rsxw = new ResXResourceWriter("en-AU.resx")
        rsxw.AddResource("en-AU.jpg",img)
        rsxw.Close()
    End Sub
End Class
using System;
using System.Drawing;
using System.Resources;
 
public class SampleClass
{
    public static void Main() 
    {
        Image img = Image.FromFile("en-AU.jpg");
        ResXResourceWriter rsxw = new ResXResourceWriter("en-AU.resx"); 
        rsxw.AddResource("en-AU.jpg",img);
        rsxw.Close();
    }
}

You can also manipulate a .resx file directly. However, to avoid corrupting the file, be careful not to modify any binary information that is stored in the file.

If you need to retrieve the names and values of the resources in a .resx file, use a ResXResourceReader. For a code example that demonstrates how to create a ResXResourceReader for a specified file, iterate through the file, and print out the names and values of resources, see the ResXResourceReader Class.

You cannot embed a .resx file in a runtime executable or compile it into a satellite assembly. You must convert your .resx file into a .resources file using the Resource File Generator (Resgen.exe). For more information, see Resources in .Resources File Format.

See Also

Reference

Resource File Generator (Resgen.exe)

Concepts

Creating Resource Files
Resources in .Resources File Format