共用方式為


.Resources 檔案格式的資源

更新:2007 年 11 月

ResourceWriter 類別是專為建立 .resources 檔而設計的。請記得,只能將物件儲存於 .resources 和 .resx 資源檔中。只有 .resources 檔案格式的資源檔可以內嵌至執行階段可執行檔,或編譯成附屬組件。您可以直接從程式碼中使用 ResourceWriter 類別,或使用資源檔產生器 (Resgen.exe) 建立 .resources 檔。

注意事項:

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

使用 ResourceWriter 類別

您可以使用 ResourceWriter 類別,直接從程式碼建立 .resources 檔。首先,以唯一檔案名稱建立 ResourceWriter。其次,呼叫各個字串的 ResourceWriter.AddResource 方法 以加入至檔案。最後,呼叫 ResourceWriter.Close 方法以將字串寫入至資源檔,並關閉 ResourceWriter。下列範例說明這個處理序。

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();
    }
}

使用 Resgen.exe

資源檔產生器 (Resgen.exe) 包裝 ResourceWriter 類別所實作的方法,以轉換 .txt 檔為 .resources 檔。Resgen.exe 也包裝有 ResourceReader,允許您使用該工具,將 .resources 檔轉換回 .txt 檔。

注意事項:

當 Resgen.exe 讀取文字檔時,註解會遺失,而且不會寫入所產生的 .resources 或 .resx 檔案。

如果文字檔包含重複的資源名稱,則 Resgen.exe 將會發出警告,並忽略重複的名稱。

下列 Resgen.exe 命令會從輸入檔案 strings.txt 建立資源檔 strings.resources。

resgen strings.txt

如果您希望輸出檔案的名稱與輸入檔案不同,必須明確指定輸出檔案的名稱。下列命令會從輸入檔案 strings.txt 建立資源檔 MyApp.resources。

resgen strings.txt MyApp.resources

下列命令會從輸入檔案 strings.resources 建立文字檔 strings.txt。請注意,只有單獨包含字串的 .resources 檔案才能執行這種類型的轉換。任何物件參考不可被寫入 .txt 檔。

resgen strings.resources strings.txt

Resgen.exe 包裝 ResourceWriter 類別所實作的方法,轉換 .resx 檔為 .resources 檔。Resgen.exe 也包裝 ResourceReader,允許您使用工具,將 .resources 檔轉換回 .resx 檔。

下列 Resgen.exe 命令會從輸入檔案 items.resx 建立資源檔 items.resources。

resgen items.resx

下列命令會從輸入檔案 items.resources 建立 .resx 檔 items.resx。請注意,將 .resx 檔案轉為 .resources 檔案時,所有物件都會保留。

resgen items.resources items.resx
注意事項:

如果 Resgen.exe 因任何理由而失敗,則傳回值將會是 –1。

請參閱

概念

建立資源檔

文字檔格式的資源

.Resx 檔格式的資源

參考

資源檔產生器 (Resgen.exe)