Share via


.Resx 檔格式的資源

.resx 資源檔格式由在 XML 標記 (Tag) 內指定物件和字串的 XML 項目所構成。 .resx 檔的其中一個優點是,以文字編輯器 (例如 Notepad 或 Microsoft Word) 開啟 .resx 檔時,即可被寫入、剖析和操作。 檢視 .resx 檔時,當這個二進位資訊是資源資訊清單的一部分時,您可以實際看到內嵌物件 (如圖) 的二進位格式。 除了這個二進位資訊,.resx 檔是完全可閱讀而且可以維護的。

注意事項注意事項

請不要使用資源檔來存放密碼、安全性敏感的資訊或私人資料。

.resx 檔包含標頭資訊的標準集合,描述資源項目的格式,並指定用來剖析資料的 XML 的版本資訊。 下列程式碼範例說明一組標頭陳述式在 .resx 檔中的可能樣子。

<?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>

接在標頭資訊後的各個項目會以名稱/值組的格式表示,與在 .txt 檔中指定字串的方式非常類似。 .resx 格式的名稱/值組以 XML 程式碼的樣式包裝,會描述字串或物件值。 當字串加入至 .resx 檔時,會將字串名稱內嵌於 <data> 標記中,並將該值放在 <value> 標記中,如下列程式碼範例所示。

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

當物件插入 .resx 檔時,會使用相同的 <data><value> 標記來描述項目,但是 <data> 標記包含一個類型或是 MIME 類型規範。 這個類型規範會保留所儲存物件的資料型別。 如果物件由二進位資料組成,則 MIME 類型規範保留儲存的二進位資訊的基底型別 (Base64)。

注意事項注意事項

所有 .resx 檔案都使用二進位序列化格式子 (Serialization Formatter) 產生並剖析特定型別的二進位資料。因此,如果某物件的二進位序列化格式以不相容的方式變更,.resx 檔會變為無效。

下列程式碼範例會示範儲存在 .resx 檔中的 Int32 物件,和保留實際 .gif 檔中二進位資訊的點陣圖物件開頭。

<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>  

使用 ResXResourceWriter 類別

您可以使用 ResXResourceWriter 類別,直接從程式碼建立 .resx 檔。 在下列範例中,程式碼會建立一個 .resx 檔,而該 .resx 檔會將 .jpg 檔案儲存為資源檔之一。 首先,使用 Image.FromFile 方法 建立影像。 其次,以唯一檔案名稱建立 ResXResourceWriter。 呼叫每一個影像的 ResXResourceWriter.AddResource 方法以加入至檔案。 最後,呼叫 ResXResourceWriter.Close 方法,將影像資訊寫入資源檔,並關閉 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();
    }
}

您也可以直接操作 .resx 檔。 然而,為了避免損毀檔案,請小心不要修改儲存在檔案中的任何二進位資訊。

如果您需要擷取 .resx 檔案中資源的名稱和值,請使用 ResXResourceReader。 如需示範如何建立特定檔案的 ResXResourceReader、逐一查看該檔案及列印資源名稱和值的程式碼範例,請參閱 ResXResourceReader 類別

您不可以將 .resx 檔嵌入執行階段可執行檔或將它編譯成附屬組件。 您必須使用資源檔產生器 (Resgen.exe) 來轉換 .resx 檔至 .resources 檔。 如需詳細資訊,請參閱 .Resources 檔案格式的資源

請參閱

參考

Resgen.exe (資源檔產生器)

概念

建立資源檔

.Resources 檔案格式的資源