.NET 應用程式中的資源Resources in .NET apps

幾乎每個實際執行品質應用程式都必須使用資源。Nearly every production-quality app has to use resources. 資源是任何使用應用程式以邏輯方式部署的非執行檔資料。A resource is any nonexecutable data that is logically deployed with an app. 資源可能在應用程式中顯示作錯誤訊息,或做為使用者介面的一部分。A resource might be displayed in an app as error messages or as part of the user interface. 資源可以含有一些表單中的資料,包括字串、影像和永續性物件。Resources can contain data in a number of forms, including strings, images, and persisted objects. (若要將保存的物件寫入至資源檔,物件必須是可序列化的)。將您的資料儲存在資源檔中可讓您變更資料,而不需要重新編譯整個應用程式。(To write persisted objects to a resource file, the objects must be serializable.) Storing your data in a resource file enables you to change the data without recompiling your entire app. 也可讓您將資料儲存在單一位置,不需要依賴儲存在多個位置的硬式編碼資料。It also enables you to store data in a single location, and eliminates the need to rely on hard-coded data that is stored in multiple locations.

.NET Framework 與 .NET Core 提供桌建立和當地語系化資源的完整支援。The .NET Framework and .NET Core provide comprehensive support for the creation and localization of resources. 此外,.NET 也支援簡單的模型以封裝及部署當地語系化資源。In addition, .NET supports a simple model for packaging and deploying localized resources.

如需 ASP.NET 中資源的詳細資訊,請參閱 ASP.NET 網頁資源概觀For information about resources in ASP.NET, see ASP.NET Web Page Resources Overview.

建立和當地語系化資源Create and localize resources

在非當地語系化的應用程式中,您可以使用資源檔做為應用程式資料的存放庫,特別是針對原始程式碼中多個位置的硬式編碼字串。In a non-localized app, you can use resource files as a repository for app data, particularly for strings that might otherwise be hard-coded in multiple locations in source code. 大多數情況下,您建立的資源是文字 (.txt) 或 XML (.resx) 檔案,並且使用 Resgen.exe (資源檔產生器) 將它們編譯成二進位 .resources 檔。Most commonly, you create resources as either text (.txt) or XML (.resx) files, and use Resgen.exe (Resource File Generator) to compile them into binary .resources files. 然後這些檔案會由語言編譯器內嵌在應用程式的可執行檔。These files can then be embedded in the app's executable file by a language compiler. 如需有關建立資源檔的詳細資訊,請參閱建立資源檔For more information about creating resources, see Creating Resource Files.

您也可以針對特定文化特性當地語系化應用程式的資源。You can also localize your app's resources for specific cultures. 這可讓您建置應用程式的當地語系化 (轉譯) 版本。This enables you to build localized (translated) versions of your apps. 當您開發使用當地語系化資源的應用程式時,如果沒有適當的資源可供使用,您會指定文化特性做為其資源使用的中性或後援文化特性。When you develop an app that uses localized resources, you designate a culture that serves as the neutral or fallback culture whose resources are used if no suitable resources are available. 一般而言,中性文化特性的資源是儲存在應用程式的可執行檔中。Typically, the resources of the neutral culture are stored in the app's executable. 個別當地語系化文化特性的其餘資源會儲存在獨立的附屬組件。The remaining resources for individual localized cultures are stored in standalone satellite assemblies. 如需詳細資訊,請參閱建立附屬組件For more information, see Creating Satellite Assemblies.

封裝和部署資源Package and deploy resources

您在附屬組件中部署當地語系化應用程式資源。You deploy localized app resources in satellite assemblies. 附屬組件包含單一文化特性的資源。它不包含任何應用程式程式碼。A satellite assembly contains the resources of a single culture; it does not contain any app code. 在附屬組件部署模型中,您會建立應用程式,具有一個預設組件 (通常是主要組件) 和應用程式支援之每個文化特性的一個附屬組件。In the satellite assembly deployment model, you create an app with one default assembly (which is typically the main assembly) and one satellite assembly for each culture that the app supports. 因為附屬組件不是主要組件的一部分,您可以輕鬆地取代或更新對應至特定文化特性的資源,而不需要取代應用程式的主要組件。Because the satellite assemblies are not part of the main assembly, you can easily replace or update resources corresponding to a specific culture without replacing the app's main assembly.

謹慎地決定哪些資源將組成應用程式的預設資源組件。Carefully determine which resources will make up your app's default resource assembly. 因為它是主要組件的一部分,它的任何變更需要您取代主要組件。Because it is a part of the main assembly, any changes to it will require you to replace the main assembly. 如果您未提供預設資源,當資源後援處理序嘗試尋找它時,將會擲回例外狀況。If you do not provide a default resource, an exception will be thrown when the resource fallback process attempts to find it. 在設計良好的應用程式中,使用資源應該永遠不會擲回例外狀況。In a well-designed app, using resources should never throw an exception.

如需詳細資訊,請參閱封裝和部署資源文章。For more information, see the Packaging and Deploying Resources article.

擷取資源Retrieve resources

在執行階段,應用程式會根據 CultureInfo.CurrentUICulture 屬性所指定的文化特性,針對每個執行緒載入適當的當地語系化資源。At run time, an app loads the appropriate localized resources on a per-thread basis, based on the culture specified by the CultureInfo.CurrentUICulture property. 這個屬性值被衍生,如下所示︰This property value is derived as follows:

如需如何設定目前 UI 文化特性的詳細資訊,請參閱 CultureInfoCultureInfo.CurrentUICulture 參考頁面。For more information about how the current UI culture is set, see the CultureInfo and CultureInfo.CurrentUICulture reference pages.

然後,您可以使用 System.Resources.ResourceManager 類別,針對目前 UI 文化特性或特定文化特性擷取資源。You can then retrieve resources for the current UI culture or for a specific culture by using the System.Resources.ResourceManager class. 雖然 ResourceManager 類別最常用於擷取資源,System.Resources 命名空間包含您可以用來擷取資源的額外類型。Although the ResourceManager class is most commonly used for retrieving resources, the System.Resources namespace contains additional types that you can use to retrieve resources. 其中包括:These include:

  • ResourceReader 類別,可讓您列舉內嵌在組件中,或儲存在獨立二進位 .resources 檔中的資源。The ResourceReader class, which enables you to enumerate resources embedded in an assembly or stored in a standalone binary .resources file. 當您不知道資源在執行階段可用的確切名稱時,這個項目很有用。It is useful when you don't know the precise names of the resources that are available at run time.

  • ResXResourceReader 類別,可讓您從 XML (.resx) 檔擷取資源。The ResXResourceReader class, which enables you to retrieve resources from an XML (.resx) file.

  • ResourceSet 類別,可讓您擷取特定文化特性的資源,而不需要觀察後援規則。The ResourceSet class, which enables you to retrieve the resources of a specific culture without observing fallback rules. 資源可以儲存在組件或獨立二進位 .resources 檔中。The resources can be stored in an assembly or a standalone binary .resources file. 您也可以開發 IResourceReader 實作,以使用 ResourceSet 類別從其他來源擷取資源。You can also develop an IResourceReader implementation that enables you to use the ResourceSet class to retrieve resources from some other source.

  • ResXResourceSet 類別,可讓您將 XML 資源檔中的所有項目擷取至記憶體。The ResXResourceSet class, which enables you to retrieve all the items in an XML resource file into memory.

另請參閱See also