.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