Resgen.exe (資源檔產生器)Resgen.exe (Resource File Generator)

資源檔產生器 (Resgen.exe) 可以將文字檔 (.txt 或 .restext) 及 XML 架構資源格式檔 (.resx),轉換成通用語言執行平台二進位檔 (.resources),這種檔案可以嵌入至執行階段二進位可執行檔或附屬組件 The Resource File Generator (Resgen.exe) converts text (.txt or .restext) files and XML-based resource format (.resx) files to common language runtime binary (.resources) files that can be embedded in a runtime binary executable or satellite assembly. (請參閱建立資源檔)。(See Creating Resource Files.)

Resgen.exe 為執行下列工作的通用資源轉換公用程式:Resgen.exe is a general-purpose resource conversion utility that performs the following tasks:

  • 將 .txt 或 .restext 檔轉換成 .resources 或 .resx 檔 Converts .txt or .restext files to .resources or .resx files. (.restext 檔案的格式與 .txt 檔案的格式相同。(The format of .restext files is identical to the format of .txt files. 不過,.restext 副檔名可以幫助您更容易識別內含資源定義的文字檔)。However, the .restext extension helps you identify text files that contain resource definitions more easily.)

  • 將 .resources 檔轉換成文字或 .resx 檔。Converts .resources files to text or .resx files.

  • 將 .resx 檔轉換成文字或 .resources 檔。Converts .resx files to text or .resources files.

  • 將元件中的字串資源解壓縮至適用于 Windows 8. x 存放區應用程式的 .resw 檔案。Extracts the string resources from an assembly into a .resw file that is suitable for use in a Windows 8.x Store app.

  • 建立可存取個別具名資源和對於 ResourceManager 執行個體的強類型類別。Creates a strongly typed class that provides access to individual named resources and to the ResourceManager instance.

如果 Resgen.exe 因任何理由而失敗,則傳回值將會是 –1。If Resgen.exe fails for any reason, the return value is –1.

若要取得 Resgen.exe 的說明,您可以使用下列未指定選項的命令,以顯示 Resgen.exe 的命令語法和選項:To get help with Resgen.exe, you can use the following command, with no options specified, to display the command syntax and options for Resgen.exe:

resgen  

您也可以使用 /? 參數:You can also use the /? switch:

resgen /?  

如果您使用 Resgen.exe 來產生二進位 .resources 檔,您可以使用語言編譯器將二進位檔案內嵌至可執行檔元件,也可以使用 元件連結器 ( # A1) 將它們編譯成附屬元件。If you use Resgen.exe to generate binary .resources files, you can use a language compiler to embed the binary files into executable assemblies, or you can use the Assembly Linker (Al.exe) to compile them into satellite assemblies.

此工具會自動與 Visual Studio 一起安裝。This tool is automatically installed with Visual Studio. 若要執行這項工具,請使用 [Visual Studio 開發人員命令提示字元] (或 Windows 7 中的 [Visual Studio 命令提示字元])。To run the tool, use the Developer Command Prompt for Visual Studio (or the Visual Studio Command Prompt in Windows 7). 如需詳細資訊,請參閱命令提示字元For more information, see Command Prompts.

在命令提示字元中,請輸入下列項目:At the command prompt, type the following:

語法Syntax

resgen  [-define:symbol1[,symbol2,...]] [/useSourcePath] filename.extension  | /compile filename.extension... [outputFilename.extension] [/r:assembly] [/str:lang[,namespace[,class[,file]]] [/publicclass]]
resgen filename.extension [outputDirectory]  

參數Parameters

參數Parameter or switch 描述Description
/define: symbol1[, symbol2,...]/define: symbol1[, symbol2,...] 從 .NET Framework 4.5 開始,支援文字資源檔 (.txt 或 .restext) 的條件式編譯。Starting with the .NET Framework 4.5, supports conditional compilation in text-based (.txt or .restext) resource files. 如果 symbol 對應至在 #ifdef 建構內輸入文字檔中的符號,關聯字串資源會包含在 .resources 檔案中。If symbol corresponds to a symbol included in the input text file within a #ifdef construct, the associated string resource is included in the .resources file. 如果輸入文字檔包含有不是 #if ! 參數所定義之符號的 /define 陳述式,關聯的字串資源會包含在資源檔中。If the input text file includes an #if ! statement with a symbol that is not defined by the /define switch, the associated string resource is included in the resources file.

使用非文字檔案則會忽略 /define/define is ignored if it is used with non-text files. 符號會區分大小寫。Symbols are case-sensitive.

如需這個選項的詳細資訊,請參閱本主題稍後的條件式編譯資源For more information about this option, see Conditionally Compiling Resources later in this topic.
useSourcePath 具體指明輸入檔的目前目錄是用來解析相對檔案路徑。Specifies that the input file's current directory is to be used to resolve relative file paths.
/compile 可讓您指定在單一大量作業中,將多個 .resx 或文字檔轉換成多個 .resources 檔案。Enables you to specify multiple .resx or text files to convert to multiple .resources files in a single bulk operation. 如果您不指定這個選項,則只能指定一個輸入檔引數。If you do not specify this option, you can specify only one input file argument. 輸出檔案的名稱為 <檔案名稱>.resources。Output files are named filename.resources.

這個選項無法與 /str: 選項搭配使用。This option cannot be used with the /str: option.

如需這個選項的詳細資訊,請參閱本主題稍後的編譯或轉換多個檔案For more information about this option, see Compiling or Converting Multiple Files later in this topic.
/r: assembly/r: assembly 從指定的組件參考中繼資料。References metadata from the specified assembly. 在轉換 .resx 檔時,以及讓 Resgen.exe 序列化或還原序列化物件資源時使用。It is used when converting .resx files and allows Resgen.exe to serialize or deserialize object resources. 它類似 C# 和 Visual Basic 編譯器的 /reference:/r: 選項。It is similar to the /reference: or /r: options for the C# and Visual Basic compilers.
filename.extension 指定要轉換的輸入檔名稱。Specifies the name of the input file to convert. 如果您使用的是這個資料表前的命令列語法中的第一個、較長者,extension 必須是下列其中一項:If you're using the first, lengthier command-line syntax presented before this table, extension must be one of the following:

.txt 或 .restext.txt or .restext
要轉換成 .resources 或 .resx 檔的文字檔。A text file to convert to a .resources or a .resx file. 文字檔只能包含字串資源。Text files can contain only string resources. 如需檔案格式的詳細資訊,請參閱建立資源檔的<文字檔中的資源>一節。For information about the file format, see the "Resources in Text Files" section of Creating Resource Files.

.resx.resx
要轉換成 .resources 或文字檔 (.txt 或 .restext) 的 XML 架構資源檔。An XML-based resource file to convert to a .resources or a text (.txt or .restext) file.

.resources.resources
要轉換成 .resx 或文字檔 (.txt 或 .restext) 的二進位資源檔。A binary resource file to convert to a .resx or a text (.txt or .restext) file.

如果您使用的是這個資料表前的命令列語法中的第二個、較短者,extension 必須為下列其中一項:If you're using the second, shorter command-line syntax presented before this table, extension must be the following:

.exe 或 .dll。.exe or .dll
.NET Framework 元件 (可執行檔或程式庫) 其字串資源會解壓縮至 .resw 檔案,以用於開發 Windows 8. x 存放區應用程式。A .NET Framework assembly (executable or library) whose string resources are to be extracted to a .resw file for use in developing Windows 8.x Store apps.
outputFilename.extension 指定要建立之資源檔的名稱和類型。Specifies the name and type of the resource file to create.

從 .txt、.restext 或 .resx 檔案轉換至 .resource 檔時,這是選擇性的引數。This argument is optional when converting from a .txt, .restext, or .resx file to a .resources file. 如果沒有指定 outputFilename,Resgen.exe 會將 .resources 副檔名附加至輸入 filename 中,並且將檔案寫入包含 filename,extension 的目錄中。If you do not specify outputFilename, Resgen.exe appends a .resources extension to the input filename and writes the file to the directory that contains filename,extension.

從 .resources 檔轉換時,outputFilename.extension 是強制的引數。The outputFilename.extension argument is mandatory when converting from a .resources file. 在將 .resources 檔轉換成 XML 架構資源檔時指定檔案名稱及 .resx 副檔名。Specify a file name with the .resx extension when converting a .resources file to an XML-based resource file. 將 .resources 檔案轉換成文字檔時,指定檔案名稱及 .txt 或 .restext 副檔名。Specify a file name with the .txt or .restext extension when converting a .resources file to a text file. 當 .resources 檔只包含字串值時,您應將 .resources 檔轉換成 .txt 檔。You should convert a .resources file to a .txt file only when the .resources file contains only string values.
outputDirectory 針對 Windows 8. x 儲存區應用程式,指定將在其中寫入包含字串資源之 .resw 檔案的目錄 filename.extensionFor Windows 8.x Store apps, specifies the directory in which a .resw file that contains the string resources in filename.extension will be written. outputDirectory 必須已經存在。outputDirectory must already exist.
/str: language[,namespace[,classname[,filename]]]/str: language[,namespace[,classname[,filename]]] language 選項所指定的程式設計語言建立強類型資源類別檔案。Creates a strongly typed resource class file in the programming language specified in the language option. language 可包含下列其中一種常值:language can consist of one of the following literals:

- C#:c#cscsharp- For C#: c#, cs, or csharp.
- Visual Basic:vbvisualbasic- For Visual Basic: vb or visualbasic.
- VBScript:vbsvbscript- For VBScript: vbs or vbscript.
- C++:c++mccpp- For C++: c++, mc, or cpp.
- JavaScript:jsjscriptjavascript- For JavaScript: js, jscript, or javascript.

您可以使用 namespace 選項指定專案的預設命名空間,使用 classname 選項指定所產生類別的名稱,以及使用 filename 選項指定類別檔的名稱。The namespace option specifies the project's default namespace, the classname option specifies the name of the generated class, and the filename option specifies the name of the class file.

/str: 選項只允許一個輸入檔,因此無法搭配 /compile 選項使用。The /str: option allows only one input file, so it cannot be used with the /compile option.

如果有指定 namespace 但未指定 classname,此時類別名稱便會衍生自該輸出檔名稱 (例如,以底線替代句號)。If namespace is specified but classname is not, the class name is derived from the output file name (for example, underscores are substituted for periods). 這樣一來,強類型資源可能無法正確運作。The strongly typed resources might not work correctly as a result. 若要避免此問題,請同時指定類別名稱和輸出檔名稱。To avoid this, specify both class name and output file name.

如需這個選項的詳細資訊,請參閱本主題稍後的產生強型別資源類別For more information about this option, see Generating a Strongly Typed Resource Class later in this topic.
/publicClass 建立強類型資源類別做為公用類別。Creates a strongly typed resource class as a public class. 根據預設,資源類別是在 C# 中的 internal 和 Visual Basic 中的 FriendBy default, the resource class is internal in C# and Friend in Visual Basic.

如果沒有使用 /str: 選項,則會忽略這個選項。This option is ignored if the /str: option is not used.

Resgen.exe 和資源檔類型Resgen.exe and Resource File Types

要成功地用 Resgen.exe 轉換資源,文字檔和 .resx 檔必須遵守正確格式。In order for Resgen.exe to successfully convert resources, text and .resx files must follow the correct format.

文字檔 (.txt 和 .restext)Text (.txt and .restext) Files

文字檔 (.txt 或 .restext) 只能包含字串資源。Text (.txt or .restext) files may contain only string resources. 如果您所撰寫的應用程式必須將字串轉譯成多種語言,字串資源是非常有用的。String resources are useful if you are writing an application that must have strings translated into several languages. 例如,使用適當的字串資源,您可以輕易地按地區安排功能表字串。For example, you can easily regionalize menu strings by using the appropriate string resource. Resgen.exe 會讀取含有名稱/值組的文字檔,其中名稱是描述資源的字串,而值是資源字串本身。Resgen.exe reads text files that contain name/value pairs, where the name is a string that describes the resource and the value is the resource string itself.

注意

如需 .txt 或 .restext 檔格式的詳細資訊,請參閱建立資源檔的<文字檔中的資源>一節。For information about the format of .txt and .restext files, see the "Resources in Text Files" section of Creating Resource Files.

包含資源的文字檔除非其只包含基本拉丁範圍 (到 U+007F) 內的字元,否則必須以 UTF-8 或 Unicode (UTF-16) 的編碼方式保存。A text file that contains resources must be saved with UTF-8 or Unicode (UTF-16) encoding unless it contains only characters in the Basic Latin range (to U+007F). Resgen.exe 在處理使用 ANSI 編碼方式儲存的文字檔時,會移除延伸的 ANSI 字元。Resgen.exe removes extended ANSI characters when it processes a text file that is saved using ANSI encoding.

Resgen.exe 會針對重複的資源名稱檢查文字檔。Resgen.exe checks the text file for duplicate resource names. 如果文字檔包含重複的資源名稱,Resgen.exe 將會發出警告並忽略第二個值。If the text file contains duplicate resource names, Resgen.exe will emit a warning and ignore the second value.

.resx 檔.resx Files

.resx 資源檔格式由 XML 項目組成。The .resx resource file format consists of XML entries. 您可以在這些 XML 項目中指定字串資源,就像在文字檔中的方式一樣。You can specify string resources within these XML entries, as you would in text files. .resx 檔優於文字檔的原因,就是您也可以指定或內嵌物件。A primary advantage of .resx files over text files is that you can also specify or embed objects. 在檢視 .resx 檔時,您可以實際看到內嵌物件 (如圖片) 的二進位格式 (當這個二進位資訊是資源資訊清單的一部分時)。When you view a .resx file, you can see the binary form of an embedded object (for example, a picture) when this binary information is a part of the resource manifest. 與文字檔一樣,您可以使用文字編輯器開啟 .resx 檔案 (例如 [記事本] 或 Microsoft Word) 並寫入、剖析和操作其內容。As with text files, you can open a .resx file with a text editor (such as Notepad or Microsoft Word) and write, parse, and manipulate its contents. 請注意您必須對 XML 標記和 .resx 檔案結構有相當的認識才能這麼做。Note that this requires a good knowledge of XML tags and the .resx file structure. 如需 .resx 檔案格式的詳細資訊,請參閱建立資源檔<.resx 檔中的資源>一節。For more details on the .resx file format, see the "Resources in .resx Files" section of Creating Resource Files.

若要建立含有內嵌非字串物件的 .resources 檔,您必須使用 Resgen.exe 來轉換含有物件的 .resx 檔,或使用 ResourceWriter 類別提供的方法,直接從程式碼中將物件資源加入您的檔案。In order to create a .resources file that contains embedded nonstring objects, you must either use Resgen.exe to convert a .resx file containing objects or add the object resources to your file directly from code by calling the methods provided by the ResourceWriter class.

如果使用 Resgen.exe 將含有物件的 .resx 檔或 .resources 檔轉換成文字檔,則所有字串資源都會正確的轉換,但是非字串物件的資料類型也會以字串的形式寫入檔案中。If your .resx or .resources file contains objects and you use Resgen.exe to convert it to a text file, all the string resources will be converted correctly, but the data types of the nonstring objects will also be written to the file as strings. 在進行轉換時您將會遺漏內嵌物件,而 Resgen.exe 將會報告擷取資源時發生錯誤。You will lose the embedded objects in the conversion, and Resgen.exe will report that an error occurred in retrieving the resources.

資源檔案類型間的轉換Converting Between Resources File Types

當您在不同資源檔類型之間轉換時,根據來源和目標檔案的類型,Resgen.exe 可能無法執行轉換也可能會遺漏特定資源的詳細資訊。When you convert between different resource file types, Resgen.exe may not be able to perform the conversion or may lose information about specific resources, depending on the source and target file types. 從某種資源檔案類型轉換為另一種類型時,下表指定成功轉換的類型。The following table specifies the types of conversions that are successful when converting from one resource file type to another.

來源檔Convert from 轉至文字檔To text file 轉至 .resx 檔To .resx file 轉至 .resw 檔To .resw file 轉至 .resources 檔To .resources file
文字檔 (.txt 或 .restext)Text (.txt or .restext) file -- 沒有問題No issues 不支援Not supported 沒有問題No issues
.resx 檔.resx file 檔案包含非字串資源 (包括文件連結) 時轉換失敗Conversion fails if file contains non-string resources (including file links) -- 不支援Not supported 沒有問題No issues
.resources 檔.resources file 檔案包含非字串資源 (包括文件連結) 時轉換失敗Conversion fails if file contains non-string resources (including file links) 沒有問題No issues 不支援Not supported --
.exe 或 .dll 組譯碼.exe or .dll assembly 不支援Not supported 不支援Not supported 只有字串資源 (包括路徑名稱) 被當做資源Only string resources (including path names) are recognized as resources 不支援Not supported

執行特定 Resgen.exe 工作Performing Specific Resgen.exe Tasks

您可以透過不同方式來使用 Resgen.exe:將文字或 XML 架構資源檔編譯成二進位檔案、資源檔格式間的轉換和產生包裝 ResourceManager 功能並提供存取資源的類別。You can use Resgen.exe in diverse ways: to compile a text-based or XML-based resource file into a binary file, to convert between resource file formats, and to generate a class that wraps ResourceManager functionality and provides access to resources. 本節提供各項工作的詳細資訊:This section provides detailed information about each task:

將資源編譯成二進位檔Compiling Resources into a Binary File

最常見的 Resgen.exe 是用來將文字架構的資源檔 (.txt 或 .restext 檔) 或 XML 架構資源檔 (.resx 檔) 編譯成二進位 .resources 檔案。The most common use of Resgen.exe is to compile a text-based resource file (a .txt or .restext file) or an XML-based resource file (a .resx file) into a binary .resources file. 輸出檔可以由語言編譯器內嵌在主要組件或由組件連結器 (AL.exe) 內嵌在附屬組件。The output file then can be embedded in a main assembly by a language compiler or in a satellite assembly by Assembly Linker (AL.exe).

編譯資源檔的語法是:The syntax to compile a resource file is:

resgen inputFilename [outputFilename]

其中的參數:where the parameters are:

inputFilename
要編譯的資源檔檔案名稱 (包括副檔名)。The file name, including the extension, of the resource file to compile. Resgen.exe 只編譯使用 .txt、.restext 或 .resx 為副檔名的檔案。Resgen.exe only compiles files with extensions of .txt, .restext, or .resx.

outputFilename
輸出檔的名稱。The name of the output file. 如果您省略 outputFilename,Resgen.exe 會在和 inputFilename 相同的目錄下建立具有 inputFilename 之根檔案名稱的 .resources 檔。If you omit outputFilename, Resgen.exe creates a .resources file with the root file name of inputFilename in the same directory as inputFilename. 如果 outputFilename 包含一個目錄路徑,則目錄必須存在。If outputFilename includes a directory path, the directory must exist.

您可以在 .resource 檔的檔案名稱中指定並將命名空間與根檔案名稱以一個句號分隔,為 .resources 檔案提供完整命名空間。You provide a fully qualified namespace for the .resources file by specifying it in the file name and separating it from the root file name by a period. 例如,如果 outputFilenameMyCompany.Libraries.Strings.resources,則命名空間是 MyCompany.Libraries。For example, if outputFilename is MyCompany.Libraries.Strings.resources, the namespace is MyCompany.Libraries.

下列命令會讀取 Resources.txt 中的名稱/值組,並且寫入名為 Resources.resources 的二進位 .resources 檔。The following command reads the name/value pairs in Resources.txt and writes a binary .resources file named Resources.resources. 由於並未明確指定輸出檔名,因此預設會接收與輸入檔相同的名稱。Because the output file name is not specified explicitly, it receives the same name as the input file by default.

resgen Resources.txt

下列命令會讀取 Resources.restext 中的名稱/值組,並且寫入名為 StringResources.resources 的二進位 .resources 檔。The following command reads the name/value pairs in Resources.restext and writes a binary resources file named StringResources.resources.

resgen Resources.restext StringResources.resources  

下列命令會讀取 XML 架構檔案且名稱為 Resources.resx 的輸入檔,並寫入命名為 Resources.resources 的二進位 .resources 檔案。The following command reads an XML-based input file named Resources.resx and writes a binary .resources file named Resources.resources.

resgen Resources.resx Resources.resources  

資源檔類型間的轉換Converting Between Resource File Types

除了將文字或 XML 架構的資源檔編譯成二進位 .resources 檔外,Resgen.exe 能將任何支援的檔案類型轉換為其他任何支援的檔案類型。In addition to compiling text-based or XML-based resource files into binary .resources files, Resgen.exe can convert any supported file type to any other supported file type. 這表示它可以執行下列轉換:This means that it can perform the following conversions:

  • .txt 檔和 .restext 檔轉換為 .resx 檔。.txt and .restext files to .resx files.

  • .resx 檔轉換為 .txt 和 .restext 檔。.resx files to .txt and .restext files.

  • .resources 檔轉換為 .txt 檔和 .restext 檔。.resources files to .txt and .restext files.

  • .resources 檔轉換為 .resx 檔。.resources files to .resx files.

語法與上一節顯示的內容相同。The syntax is the same as that shown in the previous section.

此外,您可以使用 Resgen.exe 將 .NET Framework 元件中的內嵌資源轉換為 tor Windows 8. x 商店應用程式的 .resw 檔案。In addition, you can use Resgen.exe to convert embedded resources in a .NET Framework assembly to a .resw file tor Windows 8.x Store apps.

下列命令會讀取名為 Resources.resources 的二進位 .resources 檔案,並寫入名為 Resources.resx 的 XML 架構輸出檔案。The following command reads a binary resources file Resources.resources and writes an XML-based output file named Resources.resx.

resgen Resources.resources Resources.resx  

下列命令會讀取命名為 StringResources.txt 的文字架構資源檔,並寫入名為 LibraryResources.resx 的 XML 架構資源檔。The following command reads a text-based resources file named StringResources.txt and writes an XML-based resources file named LibraryResources.resx. 除了包含字串資源之外,.resx 檔也可以用來儲存非字串資源。In addition to containing string resources, the .resx file could also be used to store non-string resources.

resgen StringResources.txt LibraryResources.resx  

下列兩個命令會讀取名稱為 Resources.resx 的 XML 架構資源檔案,並寫入名為 Resources.txt 和 Resources.restext 的文字檔。The following two commands read an XML-based resources file named Resources.resx and write text files named Resources.txt and Resources.restext. 請注意,如果 .resx 檔內含任何嵌入的物件,就無法正確的轉換成文字檔。Note that if the .resx file contains any embedded objects, they will not be accurately converted into the text files.

resgen Resources.resx Resources.txt  
resgen Resources.resx Resources.restext  

編譯或轉換多個檔案Compiling or Converting Multiple Files

您可以使用 /compile 參數將一個資源檔清單透過單一作業轉換格式。You can use the /compile switch to convert a list of resource files from one format to another in a single operation. 語法為:The syntax is:

resgen /compile filename.extension [filename.extension...]  

下列命令會將三個檔案:StringResources.txt、TableResources.resw 和 ImageResources.resw,編譯成不同檔名的 .resources 檔案,分別為 StringResources.resources、TableResources.resources 和 ImageResources.resources。The following command compiles three files, StringResources.txt, TableResources.resw, and ImageResources.resw, into separate .resources files named StringResources.resources, TableResources.resources, and ImageResources.resources.

resgen /compile StringResources.txt TableResources.resx ImageResources.resx  

將資源匯出至 .resw 檔案Exporting Resources to a .resw File

如果您要開發 Windows 8. x 商店應用程式,您可能會想要使用現有傳統型應用程式中的資源。If you're developing a Windows 8.x Store app, you may want to use resources from an existing desktop app. 然而,這兩種應用程式支援不同的檔案格式。However, the two kinds of applications support different file formats. 在桌面應用程式中,文字檔 (.txt 或 .restext) 或 .resx 檔案的資源會編譯為二進位 .resources 檔案。In desktop apps, resources in text (.txt or .restext) or .resx files are compiled into binary .resources files. 在 Windows 8. x Store 應用程式中,.resw 檔案會編譯成二進位封裝資源索引 (PRI) 檔。In Windows 8.x Store apps, .resw files are compiled into binary package resource index (PRI) files. 您可以使用 Resgen.exe 從可執行檔或附屬元件中解壓縮資源,然後將它們寫入一或多個 .resw 檔案,以在開發 Windows 8. x 存放區應用程式時使用。You can use Resgen.exe to bridge this gap by extracting resources from an executable or a satellite assembly and writing them to one or more .resw files that can be used when developing a Windows 8.x Store app.

重要

Visual Studio 會自動處理將便攜程式庫中的資源併入 Windows 8. x 存放區應用程式所需的所有轉換。Visual Studio automatically handles all conversions necessary for incorporating the resources in a portable library into a Windows 8.x Store app. 直接使用 Resgen.exe 將元件中的資源轉換為 .resw 檔案格式,只對想要在 Visual Studio 外部開發 Windows 8 x 商店應用程式的開發人員才有興趣。Using Resgen.exe directly to convert the resources in an assembly to .resw file format is of interest only to developers who want to develop a Windows 8.x Store app outside of Visual Studio.

從組件產生 .resw 檔案的語法是:The syntax to generate .resw files from an assembly is:

resgen filename.extension  [outputDirectory]  

其中的參數:where the parameters are:

filename.extension
.NET Framework 組件的名稱 (可執行檔或 .dll)。The name of a .NET Framework assembly (an executable or .DLL). 如果檔案不包含資源,Resgen.exe 不會建立任何檔案。If the file contains no resources, Resgen.exe does not create any files.

outputDirectory
.resw 檔案欲寫入的現有目錄。The existing directory to which to write the .resw files. 如果省略 outputDirectory,.resw 檔案會寫入目前的目錄。If outputDirectory is omitted, .resw files are written to the current directory. Resgen.exe 會為每個在組件中的 .resources 檔案建立 .resw 檔案。Resgen.exe creates one .resw file for each .resources file in the assembly. .resw 檔案的根檔案名稱與 .resources 檔的根名稱相同。The root file name of the .resw file is the same as the root name of the .resources file.

下列命令會在 Win8Resources 目錄中為每個內嵌在 MyApp.exe 的 .resources 檔建立 .resw 檔案:The following command creates a .resw file in the Win8Resources directory for each .resources file embedded in MyApp.exe:

resgen MyApp.exe Win8Resources  

條件式編譯資源Conditionally Compiling Resources

從 .NET Framework 4.5 開始,Resgen.exe 支援文字檔 (.txt 和 .restext) 中字串資源的條件式編譯。Starting with the .NET Framework 4.5, Resgen.exe supports conditional compilation of string resources in text (.txt and .restext) files. 這可讓您在多個組建組態中使用單一文字資源檔。This enables you to use a single text-based resource file in multiple build configurations.

在 .txt 或 .restext 檔中,如果有定義符號,您可以使用 #ifdef#endifIn a .txt or .restext file, you use the #ifdef#endif 結構以包含二進位 .resources 檔的資源,如果未下義符號,則可以使用 #if !... #endif 結構以包含資源。construct to include a resource in the binary .resources file if a symbol is defined, and you use the #if !... #endif construct to include a resource if a symbol is not defined. 在編譯時期,您可以使用 /define: 選項緊接著符號的逗號分隔清單來定義符號。At compile time, you then define symbols by using the /define: option followed by a comma-delimited list of symbols. 這個對照會區分大小寫,/define 定義的符號大小寫必須與欲編譯的文字檔中的大小寫吻合。The comparison is cased-sensitive; the case of symbols defined by /define must match the case of symbols in the text files to be compiled.

例如,下列名為 UIResources.rext 的檔案包含名為 AppTitle 的字串資源,根據是否定義符號,可以接受三個值之一:PRODUCTIONCONSULTRETAILFor example, the following file named UIResources.rext includes a string resource named AppTitle that can take one of three values, depending on whether symbols named PRODUCTION, CONSULT, or RETAIL are defined.

#ifdef PRODUCTION  
AppTitle=My Software Company Project Manager
#endif  
#ifdef CONSULT  
AppTitle=My Consulting Company Project Manager  
#endif  
#ifdef RETAIL  
AppTitle=My Retail Store Project Manager  
#endif  
FileMenuName=File  

檔案接下來可以藉由下列命令編譯成二進位的 .resources 檔案:The file can then be compiled into a binary .resources file with the following command:

resgen /define:CONSULT UIResources.restext  

這會導致該 .resources 檔案中包含兩個字串資源。This produces a .resources file that contains two string resources. AppTitle 資源的值為 "My Consulting Company Project Manager"。The value of the AppTitle resource is "My Consulting Company Project Manager".

產生強類型資源類別Generating a Strongly Typed Resource Class

Resgen.exe 支援強類型資源 (會建立包含一組靜態唯讀屬性的類別,以封裝對資源的存取)。Resgen.exe supports strongly typed resources, which encapsulates access to resources by creating classes that contain a set of static read-only properties. 這提供直接呼叫 ResourceManager 類別的方法來擷取資源。This provides an alternative to calling the methods of the ResourceManager class directly to retrieve resources. 您可以使用 Resgen.exe 中的 /str 選項來啟用強類型資源的支援,以包裝 StronglyTypedResourceBuilder 類別的功能。You can enable strongly typed resource support by using the /str option in Resgen.exe, which wraps the functionality of the StronglyTypedResourceBuilder class. 當您指定 /str 選項時,Resgen.exe 的輸出會是一個包含強類型屬性的類別 (這類屬性與輸入參數中所參考的資源相符)。When you specify the /str option, the output of Resgen.exe is a class that contains strongly typed properties that match the resources that are referenced in the input parameter. 這個類別可對已處理檔案中可取得資源的強類型提供唯讀存取。This class provides strongly typed read-only access to the resources that are available in the file processed.

建立強類型資源的語法是:The syntax to create a strongly typed resource is:

resgen inputFilename [outputFilename] /str:language[,namespace,[classname[,filename]]] [/publicClass]  

參數為:The parameters and switches are:

inputFilename
欲產生強類型資源類別的資源檔之檔案名稱 (包括副檔名)。The file name, including the extension, of the resource file for which to generate a strongly typed resource class. 檔案可以是文字、XML 架構或二進位 .resources 檔,它可以使用 .txt、.restext、.resw 或 .resources 副檔名。The file can be a text-based, XML-based, or binary .resources file; it can have an extension of .txt, .restext, .resw, or .resources.

outputFilename
輸出檔的名稱。The name of the output file. 如果 outputFilename 包含一個目錄路徑,則目錄必須存在。If outputFilename includes a directory path, the directory must exist. 如果您省略 outputFilename,Resgen.exe 會在和 inputFilename 相同的目錄下建立具有 inputFilename 之根檔案名稱的 .resources 檔。If you omit outputFilename, Resgen.exe creates a .resources file with the root file name of inputFilename in the same directory as inputFilename.

outputFilename 可以是文字、XML 架構或二進位 .resources 檔案。outputFilename can be a text-based, XML-based, or binary .resources file. 如果 outputFilename 的副檔名與 inputFilename 的副檔名不同,Resgen.exe 會執行檔案轉換。If the file extension of outputFilename is different from the file extension of inputFilename, Resgen.exe performs the file conversion.

如果 inputFilename 為 .resources 檔案且 outputFilename 也是 .resources 檔,則 Resgen.exe 會複製 .resources 檔案。If inputFilename is a .resources file, Resgen.exe copies the .resources file if outputFilename is also a .resources file. 如果省略 outputFilename,Resgen.exe 會覆寫具有相同 .resources 檔案的 inputFilenameIf outputFilename is omitted, Resgen.exe overwrites inputFilename with an identical .resources file.

languagelanguage
針對強型別資源類別產生原始程式碼的語言。The language in which to generate source code for the strongly typed resource class. 針對 C# 程式碼,可能的值為 csC#csharp;針對 Visual Basic 程式碼,為 vbvisualbasic;針對 VBScript 程式碼,為 vbsvbscript;針對 C++ 程式碼,為 c++mccppPossible values are cs, C#, and csharp for C# code, vb and visualbasic for Visual Basic code, vbs and vbscript for VBScript code, and c++, mc, and cpp for C++ code.

命名 空間namespace
包含強類型資源類別的命名空間。The namespace that contains the strongly typed resource class. .resources 檔案和資源類別應該有相同的命名空間。The .resources file and the resource class should have the same namespace. 如需指定 outputFilename 的命名空間之詳細資訊,請參閱編譯資源至二進位檔中For information about specifying the namespace in the outputFilename, see Compiling Resources into a Binary File. 如果省略 namespace,則命名空間不會包含資源類別。If namespace is omitted, the resource class is not contained in a namespace.

classnameclassname
強類型資源類別的名稱。The name of the strongly typed resource class. 這應該會對應於 .resources 檔的根名稱。This should correspond to the root name of the .resources file. 例如,如果 Resgen.exe 產生名為 MyCompany.Libraries.Strings.resources 的 .resources 檔案,強類型資源類別的名稱會為 Strings。For example, if Resgen.exe generates a .resources file named MyCompany.Libraries.Strings.resources, the name of the strongly typed resource class is Strings. 如果省略 classname,產生的類別會衍生自 outputFilename 的根目錄名稱。If classname is omitted, the generated class is derived from the root name of outputFilename. 如果省略 outputFilename,產生的類別會衍生自 inputFilename 的根目錄名稱。If outputFilename is omitted, the generated class is derived from the root name of inputFilename.

classname 無法包含無效的字元 (如空白字元)。classname cannot contain invalid characters such as embedded spaces. 如果 classname 包含空白字元,或如果 classname 預設是由 inputFilename 產生,且 inputFilename 包含空白字元,則 Resgen.exe 會以底線 (_) 取代所有無效的字元。If classname contains embedded spaces, or if classname is generated by default from inputFilename, and inputFilename contains embedded spaces, Resgen.exe replaces all invalid characters with an underscore (_).

filenamefilename
類別檔案的名稱。The name of the class file.

/publicclass
讓強類型資源類別為公用類別而不是 internal (C#) 或 Friend (Visual Basic)。Makes the strongly typed resource class public rather than internal (in C#) or Friend (in Visual Basic). 這可讓資源從它們的內嵌組件外部存取。This allows the resources to be accessed from outside the assembly in which they are embedded.

重要

當您建立強類型資源類別時,您的 .resources 檔案的名稱必須符合所產生程式碼的命名空間和類別名稱。When you create a strongly typed resource class, the name of your .resources file must match the namespace and class name of the generated code. 不過,Resgen.exe 可讓您指定所產生 .resources 檔案有不相容名稱的選項。However, Resgen.exe allows you to specify options that produce a .resources file that has an incompatible name. 為了解決這個行為,可在它產生之後重新命名輸出檔案。To work around this behavior, rename the output file after it has been generated.

強類型資源類別具有下列成員:The strongly typed resource class has the following members:

  • 無參數的函式,可用來具現化強型別資源類別。A parameterless constructor, which can be used to instantiate the strongly typed resource class.

  • static (C#) 或 Shared (Visual Basic) 和唯讀 ResourceManager 屬性,可傳回 ResourceManager 管理強類型資源的執行個體。A static (C#) or Shared (Visual Basic) and read-only ResourceManager property, which returns the ResourceManager instance that manages the strongly typed resource.

  • 靜態 Culture 屬性,可讓您將文化特性設定為資源擷取使用。A static Culture property, which allows you to set the culture used for resource retrieval. 根據預設,它的值是為 null,表示已使用目前的 UI 文化特性。By default, its value is null, which means that the current UI culture is used.

  • 在 .resource 檔中每個 resource 的 static (C#) 或 Shared (Visual Basic) 和唯讀屬性。One static (C#) or Shared (Visual Basic) and read-only property for each resource in the .resources file. 屬性名稱是資源的名稱。-The name of the property is the name of the resource.-

例如,下列命令會將名為 StringResources.txt 編譯為 StringResources.resources,並且在一個名為 StringResources.vb 的 Visual Basic 原始程式碼中產生名為 StringResources 的類別,可用於存取資源管理員。For example, the following command compiles a resource file named StringResources.txt into StringResources.resources and generates a class named StringResources in a Visual Basic source code file named StringResources.vb that can be used to access the Resource Manager.

resgen StringResources.txt /str:vb,,StringResources

另請參閱See also