共用方式為


字串資源

若要建立字串資源,請執行下列三個步驟:

  1. 建立一個文字檔,裡面的每個字串都有名稱值項目。

  2. 使用 Resgen.exe,將文字檔轉換為 .resources 檔案。

  3. 建立 DLL,使用組件連結器 (AL) 或 .NET Framework SDK 提供的某個語言編譯器,嵌入 .resources 檔案。

    **注意   **有關 AL 的詳細資訊,請參閱附錄 B:資源工具

若要將這些字串當地語系化成非預設的文化特性,您必須對每個文化特性和預設中性語言文化特性,執行前三個步驟。使用 AL 時,還必須指定文化特性。

清單 1. 字串文字檔範例 (MyStrings.txt)

; Sample strings for the calculator – Default locale
;
Math_Greeting = Welcome – Integer Calculator
Math_Formula_Label = Formula and Results:
Math_Clear_Button = Clear
Math_Calc_Button = Calculate
Math_Calc_Error = Invalid Formula. Please try again.

Resgen.exe 可以在三個資源檔案格式之間轉換:字串 (.txt 檔案)、XML 資源 (.resx 檔案) 和編譯 (.resources 檔案)。若要編譯 MyStrings.txt 資源檔案,請使用下列命令列產生一個有多種資源的中間編譯檔:

resgen MyStrings.txt MyStrings.resources

此時您有三個封裝選項:將資源嵌入到可執行組件中、建立用來保留資源的附屬組件,或是將這些資源留在 .resources 檔案中直接存取。工具大多使用最後一個選項,真正的應用程式應該使用組件來發揮簽章和版本檢查的功用。

許多應用程式 - 尤其是沒有當地語系化的應用程式 - 只需將資源嵌入到可執行組件中。您只要在編譯時使用 /res 及下列語法,即可完成:

(csc|vbc) ... /out:WorldCalc.exe /res:MyStrings.resources WorldCalc.cs

注意   為簡明起見,上一行省略了 /target/addmodule/a/r 參數。

上一個命令列將 WorldCalc.cs 編譯為 WorldCalc.exe,並且從 MyStrings.resources 檔案嵌入資源。您可以使用類似下面的命令列,將資源放入別的 DLL 中:

al /out:WorldCalc.Resources.DLL /embed:MyStrings.resources,MyStrings.resources,Private

/embed 參數使用下列語法:

/embed:<filename>[,<name>[,Private]

請參閱下表介紹的參數。

  • <filename> - .resources 檔案的名稱。

  • <name> - 資源的內部名稱。通常在建構 ResourceManager 時使用。大多有一個命名空間。

  • Private - 其他組件能否看到這些資源 (預設值是 no)。

    **注意   **您可以使用 /link 參數,而非 /embed。它可以建置資源組件 DLL (內容資訊清單),但它是連結至 .resources 檔案,而非嵌入資源本身。

使用程式碼建立資源

您可以使用程式碼,直接建立 .resource 檔案。這個技術在自動產生適當的格式化資源時,十分有用,尤其是資源存放在資料庫中或是從其他系統 (例如,使用舊版 Win32 字串資源表) 移轉而來。若要建立 .resources 檔案,請用唯一的檔案名稱建立 ResourceWriter,並且至少呼叫一次 AddResource,然後呼叫 Close (它會隱含呼叫 Generate) 來關閉檔案。下列小型程式說明如何使用 ResourceWriter 建立有五個項目的 .resources 檔案。

清單 2a. 建立及讀取 .resource 檔案 (ResWrite.cs)

using System;
using System.Collections;
using System.Resources;

class MainApp {
   public static void Main() {
      // First create the resource file and add strings.
      IResourceWriter rw = new ResourceWriter("sample.resources");
      rw.AddResource("test1", "one");
      rw.AddResource("test2", "two");
      rw.AddResource("test3", "three");
      rw.AddResource("test4", "four");
      rw.AddResource("test5", 512341234);
      rw.Close();
      ...
   }
}

清單 2b. 建立及讀取 .resource 檔案 (ResWrite.vb)

Imports System
Imports System.Collections
Imports System.Resources

Class MainApp
    
   Public Shared Sub Main()
      ' First create the resource file and add strings.
      Dim rw As ResourceWriter = New ResourceWriter("sample.resources")
      rw.AddResource("test1", "one")
      rw.AddResource("test2", "two")
      rw.AddResource("test3", "three")
      rw.AddResource("test4", "four")
      rw.AddResource("test5", 512341234)
      rw.Close()
      ...
   End Sub
End Class

**注意   **您應該將它指定給 IResourceWriter,確保程式碼仍然使用其他支援相同介面的 ResourceWriter 類別。

擷取檔案資源的最簡單方法是使用 ResourceReader 類別來加以重複。下面介紹如何根據前述程式碼,建置最基本的程式碼來列舉這些撰寫好的資源 (取自相同的 ResWrite.csResWrite.vb 程式檔案):

清單 3a. 使用 ResourceReader 重複 (ResWrite.cs)

      ...
      // Iterate through the resources.
      IResourceReader rr = new ResourceReader("sample.resources");
      IDictionaryEnumerator de = rr.GetEnumerator();
      while (de.MoveNext()) {
            Console.WriteLine(de.Key + " " + de.Value);
      }
      rr.Close();
      ...

清單 3a. 使用 ResourceReader 重複 (ResWrite.vb)

      ...
      ' Iterate through the resources.
      Dim rr As ResourceReader = New ResourceReader("sample.resources")
      Dim de As IDictionaryEnumerator = rr.GetEnumerator()
      While de.MoveNext()
         Console.WriteLine((de.Key.ToString() + " " + de.Value.ToString()))
      End While
      rr.Close()
      ...

在這個小型範例中,您先為先前建立的 .resources 檔案,建立 ResourceReader。撰寫資源時,再將它指定給 IResourceReader 來提高靈活度。接著再使用 DictionaryEnumerator 來逐步執行資源、列印資源名稱以及將值對應到主控台。

雖然直接使用 ResourceReader 可以列舉資源,但卻不能指定命名項目來直接擷取特定資源。您可以使用使用程式碼擷取資源章節中的強大 ResourceManager 來完成這個作業。

請參閱

影像資源 | 封裝資源 | 使用程式碼擷取資源 | 資源摘要 | 附錄 A:其他資源資訊 | 附錄 B:資源工具