Regasm.exe (組件登錄工具)Regasm.exe (Assembly Registration Tool)

組件註冊工具會讀取組件內的中繼資料,並將必要的項目加入至登錄,這樣可讓 COM 用戶端順利地建立 .NET Framework 類別。The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. 註冊類別之後,任何 COM 用戶端都可以將它當做 COM 類別使用。Once a class is registered, any COM client can use it as though the class were a COM class. 類別只會在安裝組件時註冊一次。The class is registered only once, when the assembly is installed. 在實際註冊類別之後,才能從 COM 建立組件內類別的執行個體。Instances of classes within the assembly cannot be created from COM until they are actually registered.

若要執行此工具,請使用 Visual Studio 開發人員命令提示字元或 Visual Studio 開發人員 PowerShellTo run the tool, use Visual Studio Developer Command Prompt or Visual Studio Developer PowerShell.

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

語法Syntax

regasm assemblyFile [options]

參數Parameters

參數Parameter DescriptionDescription
assemblyFileassemblyFile 要向 COM 註冊的組件。The assembly to be registered with COM.
選項Option DescriptionDescription
/codebase/codebase 在登錄中建立一個程式碼基底項目。Creates a Codebase entry in the registry. 程式碼基底專案會指定未安裝在全域組件快取中之元件的檔案路徑。The Codebase entry specifies the file path for an assembly that's not installed in the global assembly cache. 如果您稍後要將登錄的元件安裝到全域組件快取中,請不要指定此選項。Don't specify this option if you will subsequently install the assembly that you're registering into the global assembly cache. 您使用 /codebase 選項指定的 assemblyFile 引數必須是 強式名稱組件The assemblyFile argument that you specify with the /codebase option must be a strong-named assembly.
/registered/registered 指定這個工具只會參考已註冊的類型程式庫。Specifies that this tool will only refer to type libraries that have already been registered.
/asmpath:directory/asmpath:directory 指定包含組件參考的目錄。Specifies a directory containing assembly references. 必須與 /regfile 選項一起使用。Must be used with the /regfile option.
/nologo/nologo 隱藏 Microsoft 程式啟始資訊顯示。Suppresses the Microsoft startup banner display.
/regfile [ regfile]/regfile [: regFile] 產生組件的指定 .reg 檔,其中包含所需的登錄項目。Generates the specified .reg file for the assembly, which contains the needed registry entries. 指定這個選項並不會變更登錄。Specifying this option does not change the registry. 這個選項不可與 /u/tlb 選項一起使用。You cannot use this option with the /u or /tlb options.
/silent/s/silent or /s 隱藏顯示成功訊息。Suppresses the display of success messages.
/tlb [ typeLibFile]/tlb [: typeLibFile] 從指定的組件中產生類型程式庫,其中包含組件內所定義之可存取類型的定義。Generates a type library from the specified assembly containing definitions of the accessible types defined within the assembly.
/unregister/u/unregister or /u 移除註冊 assemblyFile 中所找到的可建立類別。Unregisters the creatable classes found in assemblyFile. 省略這個選項會造成 Regasm.exe 註冊組件中可建立的類別。Omitting this option causes Regasm.exe to register the creatable classes in the assembly.
/verbose/verbose 指定詳細資訊模式,與 /tlb 選項一起指定時,顯示需要產生型別程式庫之任何參考組件的清單。Specifies verbose mode; displays a list of any referenced assemblies for which a type library needs to be generated, when specified with the /tlb option.
/?/? /helpor /help 顯示工具的命令語法和選項。Displays command syntax and options for the tool.

注意

Regasm.exe 命令列選項不區分大小寫。The Regasm.exe command-line options are case insensitive. 您只需要提供足夠的選項資訊,就可唯一識別該選項。You only need to provide enough of the option to uniquely identify it. 例如,/n 相當於 /nologo,且 /t: outfile.tlb 相當於 /tlb: outfile.tlbFor example, /n is equivalent to /nologo and /t: outfile.tlb is equivalent to /tlb: outfile.tlb.

備註Remarks

您可以使用 /regfile 選項產生包含登錄項目的 .reg 檔,而不是直接變更登錄。You can use the /regfile option to generate a .reg file that contains the registry entries instead of making the changes directly to the registry. 您可以使用登錄編輯程式工具 (Regedit.exe) 匯入 .reg 檔,藉此更新電腦上的登錄。You can update the registry on a computer by importing the .reg file with the Registry Editor tool (Regedit.exe). .Reg 檔案不包含任何可由使用者定義的暫存器函數進行的登錄更新。The .reg file does not contain any registry updates that can be made by user-defined register functions. /Regfile 選項只會發出 managed 類別的登錄專案。The /regfile option only emits registry entries for managed classes. 這個選項不會發出 TypeLibIDInterfaceID 的項目。This option does not emit entries for TypeLibIDs or InterfaceIDs.

當您指定 /tlb 選項時,Regasm.exe 會產生及註冊型別程式庫,描述組件中找到的類型。When you specify the /tlb option, Regasm.exe generates and registers a type library describing the types found in the assembly. Regasm.exe 會將產生的類型程式庫放入目前的工作目錄中,或是為輸出檔指定的目錄中。Regasm.exe places the generated type libraries in the current working directory or the directory specified for the output file. 為參考其他組件的組件產生類型程式庫可能導致一次產生多個類型程式庫。Generating a type library for an assembly that references other assemblies may cause several type libraries to be generated at once. 您可以使用型別程式庫提供型別資訊給 Visual Studio 這類開發工具。You can use the type library to provide type information to development tools like Visual Studio. 如果您要註冊的元件是由類型程式庫匯入工具 (Tlbimp.exe) 所產生,請勿使用 /tlb 選項。Don't use the /tlb option if the assembly you are registering was produced by the Type Library Importer (Tlbimp.exe). 您無法從原本自類型程式庫匯入的組件匯出類型程式庫。You cannot export a type library from an assembly that was imported from a type library. 使用 /tlb 選項與使用類型程式庫匯出工具 (Tlbexp.exe) 和 Regasm.exe 的效果相同,但是有一點除外,就是 Tlbexp.exe 不會註冊本身所產生的類型資料庫。Using the /tlb option has the same effect as using the Type Library Exporter (Tlbexp.exe) and Regasm.exe, with the exception that Tlbexp.exe does not register the type library it produces. 如果您使用 /tlb 選項來註冊類型程式庫,您可以搭配使用 /tlb 選項與 /unregister 選項來取消註冊類型程式庫。If you use the /tlb option to register a type library, you can use the /tlb option with the /unregister option to unregister the type library. 這兩個選項一起使用時,將會移除類型程式庫和介面項目的註冊,因而大幅清除登錄。Using the two options together will unregister the type library and interface entries, which can clean the registry considerably.

當您註冊 COM 要使用的組件時,Regasm.exe 會將項目加入至本機電腦上的登錄。When you register an assembly for use by COM, Regasm.exe adds entries to the registry on the local computer. 更精確地說,它會建立與版本相關的登錄機碼,允許相同組件的多個版本在電腦上並存執行。More specifically, it creates version-dependent registry keys that allow multiple versions of the same assembly to run side by side on a computer. 第一次註冊元件時,系統會為元件建立一個最上層索引鍵,並且針對特定版本建立唯一的子機碼。The first time an assembly is registered, one top-level key is created for the assembly, and a unique subkey is created for the specific version. 每次您註冊新的組件版本時,Regasm.exe 就會為這個新的版本建立一個子機碼。Each time you register a new version of the assembly, Regasm.exe creates a subkey for the new version.

例如,假設您註冊 Managed 元件 myComp.dll 1.0.0.0 版以供 COM 使用。For example, consider a scenario where you register the managed component, myComp.dll, version 1.0.0.0 for use by COM. 稍後您又註冊 myComp.dll 2.0.0.0 版。Later, you register myComp.dll, version 2.0.0.0. 您決定讓電腦上的所有 COM 用戶端應用程式都使用 myComp.dll 2.0.0.0 版,並決定移除 myComponent.dll 1.0.0.0 版的註冊。You determine that all COM client applications on the computer are using myComp.dll version 2.0.0.0 and you decide to unregister myComponent.dll version 1.0.0.0. 這項登錄配置可讓您移除 myComp.dll 1.0.0.0 版的註冊,因為只會移除 1.0.0.0 版的子機碼。This registry scheme allows you to unregister myComp.dll version 1.0.0.0 because only the version 1.0.0.0 subkey is removed.

使用 Regasm.exe 註冊組件之後,您可以將它安裝在全域組件快取中,以便可以從任何 COM 用戶端將它啟用。After registering an assembly using Regasm.exe, you can install it in the global assembly cache so that it can be activated from any COM client. 如果組件將只會由單一應用程式啟用,您可以將它放置到該應用程式的目錄中。If the assembly is only going to be activated by a single application, you can place it in that application's directory.

範例Examples

下列命令會註冊所有包含在 myTest.dll 中的公用類別。The following command registers all public classes contained in myTest.dll.

regasm myTest.dll

下列命令會產生 myTest.reg 檔案,該檔案中包含所有必要的登錄項目。The following command generates the file myTest.reg, which contains all the necessary registry entries. 這個命令不會更新登錄。This command does not update the registry.

regasm myTest.dll /regfile:myTest.reg

下列命令會註冊包含在 myTest.dll 檔中的所有公用類別,並且產生和註冊 myTest.tlb 類型程式庫,其中包含所有在 myTest.dll 中定義之公用類型的定義。The following command registers all public classes contained in myTest.dll, and generates and registers the type library myTest.tlb, which contains definitions of all the public types defined in myTest.dll.

regasm myTest.dll /tlb:myTest.tlb

另請參閱See also