ResourceManager.GetString 方法

定义

返回指定区域性或当前 UI 区域性的指定字符串资源。Returns the specified string resource for the specified culture or current UI culture.

重载

GetString(String)

返回指定的字符串资源的值。Returns the value of the specified string resource.

GetString(String, CultureInfo)

返回为指定区域性本地化的字符串资源的值。Returns the value of the string resource localized for the specified culture.

GetString(String)

返回指定的字符串资源的值。Returns the value of the specified string resource.

public:
 virtual System::String ^ GetString(System::String ^ name);
public virtual string GetString (string name);
abstract member GetString : string -> string
override this.GetString : string -> string
Public Overridable Function GetString (name As String) As String

参数

name
String

要检索的资源的名称。The name of the resource to retrieve.

返回

为调用方的当前 UI 区域性本地化的资源的值,如果在资源集中找不到 name,则为 nullThe value of the resource localized for the caller's current UI culture, or null if name cannot be found in a resource set.

异常

name 参数为 nullThe name parameter is null.

指定资源的值不是字符串。The value of the specified resource is not a string.

未找到可用的资源集,并且没有默认区域性的资源。No usable set of resources has been found, and there are no resources for the default culture. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

示例

下面的示例使用GetString方法检索特定于区域性的资源。The following example uses the GetString method to retrieve culture-specific resources. 它包含从 .txt 文件中编译的、英语(en)、法语(法国)(fr)和俄语(俄罗斯)(ru)区域性的资源。It consists of resources compiled from .txt files for the English (en), French (France) (fr-FR), and Russian (Russia) (ru-RU) cultures. 该示例将当前区域性和当前 UI 区域性更改为英语(美国)、法语(法国)、俄语(俄罗斯)和瑞典语(瑞典)。The example changes the current culture and current UI culture to English (United States), French (France), Russian (Russia), and Swedish (Sweden). 然后,它调用GetString方法来检索本地化字符串,该字符串与当前日期和月份一起显示。It then calls the GetString method to retrieve the localized string, which it displays along with the current day and month. 请注意,输出将显示适当的本地化字符串,除非当前 UI 区域性为 "瑞典语(瑞典)"。Notice that the output displays the appropriate localized string except when the current UI culture is Swedish (Sweden). 由于瑞典语资源不可用,因此应用会改用默认区域性的资源,即英语。Because Swedish language resources are unavailable, the app instead uses the resources of the default culture, which is English. 该示例需要下表中列出的基于文本的资源文件。The example requires the text-based resource files listed in following table. 每个都有一个名为DateStart的字符串资源。Each has a single string resource named DateStart.

cultureCulture 文件名File name 资源名称Resource name 资源值Resource value
en-USen-US DateStrings.txtDateStrings.txt DateStart 今天是Today is
fr-FRfr-FR DateStrings.fr-FR.txtDateStrings.fr-FR.txt DateStart Aujourd'hui,c'est leAujourd'hui, c'est le
ru-RUru-RU DateStrings.ru-RU.txtDateStrings.ru-RU.txt DateStart СегодняСегодня

您可以使用以下批处理文件编译该C#示例。You can use the following batch file to compile the C# example. 对于 Visual Basic,将 csc 更改为 vbc,并将源代码文件的扩展名由 .cs 更改为 .vbFor Visual Basic, change csc to vbc, and change the extension of the source code file from .cs to .vb.

resgen DateStrings.txt  
csc showdate.cs /resource:DateStrings.resources  
  
md fr-FR  
resgen DateStrings.fr-FR.txt  
al /out:fr-FR\Showdate.resources.dll /culture:fr-FR /embed:DateStrings.fr-FR.resources   
  
md ru-RU  
resgen DateStrings.ru-RU.txt  
al /out:ru-RU\Showdate.resources.dll /culture:ru-RU /embed:DateStrings.ru-RU.resources  

下面是此示例的源代码(适用于该C#版本的 Visual Basic 版本的 ShowDate 或 ShowDate.cs)。Here's the source code for the example (ShowDate.vb for the Visual Basic version or ShowDate.cs for the C# version).

using System;
using System.Globalization;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguage("en")]

public class Example
{
   public static void Main()
   {
      string[] cultureNames = { "en-US", "fr-FR", "ru-RU", "sv-SE" };
      ResourceManager rm = new ResourceManager("DateStrings",
                                               typeof(Example).Assembly);
      
      foreach (var cultureName in cultureNames) {
         CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
         Thread.CurrentThread.CurrentCulture = culture; 
         Thread.CurrentThread.CurrentUICulture = culture;

         Console.WriteLine("Current UI Culture: {0}", 
                           CultureInfo.CurrentUICulture.Name);
         string dateString = rm.GetString("DateStart");
         Console.WriteLine("{0} {1:M}.\n", dateString, DateTime.Now);                           
      }                                           
   }
}
// The example displays output similar to the following:
//       Current UI Culture: en-US
//       Today is February 03.
//       
//       Current UI Culture: fr-FR
//       Aujourd'hui, c'est le 3 février
//       
//       Current UI Culture: ru-RU
//       Сегодня февраля 03.
//       
//       Current UI Culture: sv-SE
//       Today is den 3 februari.
Imports System.Globalization
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguage("en")>

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR", "ru-RU", "sv-SE" }
      Dim rm As New ResourceManager("DateStrings",
                                    GetType(Example).Assembly)
      
      For Each cultureName In cultureNames
         Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
         Thread.CurrentThread.CurrentCulture = culture 
         Thread.CurrentThread.CurrentUICulture = culture

         Console.WriteLine("Current UI Culture: {0}", 
                           CultureInfo.CurrentUICulture.Name)
         Dim dateString As String = rm.GetString("DateStart")
         Console.WriteLine("{0} {1:M}.", dateString, Date.Now)                           
         Console.WriteLine()
      Next                                           
   End Sub
End Module
' The example displays output similar to the following:
'       Current UI Culture: en-US
'       Today is February 03.
'       
'       Current UI Culture: fr-FR
'       Aujourd'hui, c'est le 3 février
'       
'       Current UI Culture: ru-RU
'       Сегодня февраля 03.
'       
'       Current UI Culture: sv-SE
'       Today is den 3 februari.

注解

桌面应用Desktop Apps

在桌面应用中,为当前线程的 UI 区域性本地化返回的资源,由CultureInfo.CurrentUICulture属性定义。In desktop apps, the resource that is returned is localized for the UI culture of the current thread, as defined by the CultureInfo.CurrentUICulture property. 如果尚未针对该区域性对资源进行本地化,则 resource manager 会按照打包和部署资源一文中的 "资源回退进程" 一节中所述的步骤来探测资源。If the resource has not been localized for that culture, the resource manager probes for a resource by following the steps outlined in the "Resource Fallback Process" section of the Packaging and Deploying Resources article. 如果未找到可用的本地化资源集,资源管理器将回退到默认区域性的资源。If no usable set of localized resources is found, the resource manager falls back on the default culture's resources. 如果资源管理器无法加载默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If the resource manager cannot load the default culture's resource set, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写(默认值)或区分大小写。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

注意

此方法可能会引发比列出的更多异常。This method can throw more exceptions than are listed. 出现这种情况的原因之一是,此方法调用的方法会引发异常。One reason this might occur is if a method that this method calls throws an exception. 例如, FileLoadException如果在部署或安装附属程序集时出错,则可能会引发异常; 如果在反SerializationException序列化类型时用户定义的类型引发用户定义的异常,则可能引发异常。For example, a FileLoadException exception might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException exception might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

Windows 8.x 应用商店Windows 8.x Store 应用程序Apps

重要

尽管此ResourceManager类在应用中Windows 8.x 应用商店Windows 8.x Store受支持,但我们不建议使用它。Although the ResourceManager class is supported in Windows 8.x 应用商店Windows 8.x Store apps, we do not recommend its use. 仅当你开发可移植类库Portable Class LibraryWindows 8.x 应用商店Windows 8.x Store用于应用程序的项目时,才使用此类。Use this class only when you develop 可移植类库Portable Class Library projects that can be used with Windows 8.x 应用商店Windows 8.x Store apps. 若要从Windows 8.x 应用商店Windows 8.x Store应用中检索资源,请改用windows.applicationmodel.resources.core. windows.applicationmodel.resources.resourceloader类。To retrieve resources from Windows 8.x 应用商店Windows 8.x Store apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.

Windows 8.x 应用商店Windows 8.x Store应用程序中GetString(String) ,该方法name返回字符串资源的值,该资源已本地化为调用方的当前 UI 区域性设置。In Windows 8.x 应用商店Windows 8.x Store apps, the GetString(String) method returns the value of the name string resource, localized for the caller's current UI culture settings. 区域性列表派生自操作系统的首选 UI 语言列表。The list of cultures is derived from the operating system's preferred UI language list. 如果资源管理器不匹配name,则该方法null返回。If the resource manager cannot match name, the method returns null.

安全性

ReflectionPermission
当通过诸如InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])等机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联的枚举:MemberAccessAssociated enumeration: MemberAccess.

线程安全性

方法GetString(String)是线程安全的。The GetString(String) method is thread safe.

另请参阅

GetString(String, CultureInfo)

返回为指定区域性本地化的字符串资源的值。Returns the value of the string resource localized for the specified culture.

public:
 virtual System::String ^ GetString(System::String ^ name, System::Globalization::CultureInfo ^ culture);
public virtual string GetString (string name, System.Globalization.CultureInfo culture);
abstract member GetString : string * System.Globalization.CultureInfo -> string
override this.GetString : string * System.Globalization.CultureInfo -> string
Public Overridable Function GetString (name As String, culture As CultureInfo) As String

参数

name
String

要检索的资源的名称。The name of the resource to retrieve.

culture
CultureInfo

一个对象,表示为其本地化资源的区域性。An object that represents the culture for which the resource is localized.

返回

为指定区域性本地化的资源的值,如果在资源集中找不到 null,则为 nameThe value of the resource localized for the specified culture, or null if name cannot be found in a resource set.

异常

name 参数为 nullThe name parameter is null.

指定资源的值不是字符串。The value of the specified resource is not a string.

未找到可用的资源集,并且没有默认区域性的资源。No usable set of resources has been found, and there are no resources for a default culture. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

默认区域性的资源位于无法找到的附属程序集。The default culture's resources reside in a satellite assembly that could not be found. 有关如何处理此异常的信息,请参阅 ResourceManager 类主题“处理 MissingManifestResourceException 和 MissingSatelliteAssemblyException 异常”一节。For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

示例

下面的示例使用GetString(String, CultureInfo)方法检索特定于区域性的资源。The following example uses the GetString(String, CultureInfo) method to retrieve culture-specific resources. 该示例的默认区域性为英语(en),它包括法语(法国)(fr-fr)和俄语(俄罗斯)(ru)区域性的附属程序集。The example's default culture is English (en), and it includes satellite assemblies for the French (France) (fr-FR) and Russian (Russia) (ru-RU) cultures. 在调用GetString(String, CultureInfo)之前,此示例将当前区域性和当前 UI 区域性更改为俄语(俄罗斯)。The example changes the current culture and current UI culture to Russian (Russia) before calling GetString(String, CultureInfo). 然后,它调用GetString方法DateTime.ToString(String, IFormatProvider)和方法,并将CultureInfo表示法语(法国)和瑞典语(瑞典)区域性的对象传递给每个方法。It then calls the GetString method and the DateTime.ToString(String, IFormatProvider) method and passes CultureInfo objects that represent the French (France) and Swedish (Sweden) cultures to each method. 在输出中,月份和月份的日期以及前面的字符串显示为法语,因为GetString方法可以检索法语资源。In the output, the month and day of the month as well as the string that precedes them appear in French, because the GetString method is able to retrieve the French language resource. 但是,当使用瑞典语(瑞典)区域性时,月份和月份中的某一天将以瑞典语显示,但其前面的字符串为英语。However, when the Swedish (Sweden) culture is used, the month and day of the month appear in Swedish, although the string that precedes them is in English. 这是因为资源管理器找不到本地化的瑞典语资源,因此它将返回默认英语区域性的资源。This is because the resource manager cannot find localized Swedish language resources, so it returns a resource for the default English culture instead.

该示例需要下表中列出的基于文本的资源文件。The example requires the text-based resource files listed in following table. 每个都有一个名为DateStart的字符串资源。Each has a single string resource named DateStart.

cultureCulture 文件名File name 资源名称Resource name 资源值Resource value
en-USen-US DateStrings.txtDateStrings.txt DateStart 今天是Today is
fr-FRfr-FR DateStrings.fr-FR.txtDateStrings.fr-FR.txt DateStart Aujourd'hui,c'est leAujourd'hui, c'est le
ru-RUru-RU DateStrings.ru-RU.txtDateStrings.ru-RU.txt DateStart СегодняСегодня

可以使用以下批处理文件编译 Visual Basic 示例。You can use the following batch file to compile the Visual Basic example. 若要在C#中编译vbccsc请将更改为,并将源代码文件的扩展名.vb.cs更改为。To compile in C#, change vbc to csc, and change the extension of the source code file from .vb to .cs.

resgen DateStrings.txt  
vbc showdate.vb /resource:DateStrings.resources  
  
md fr-FR  
resgen DateStrings.fr-FR.txt  
al /out:fr-FR\Showdate.resources.dll /culture:fr-FR /embed:DateStrings.fr-FR.resources   
  
md ru-RU  
resgen DateStrings.ru-RU.txt  
al /out:ru-RU\Showdate.resources.dll /culture:ru-RU /embed:DateStrings.ru-RU.resources  

下面是此示例的源代码(适用于该C#版本的 Visual Basic 版本的 ShowDate 或 ShowDate.cs)。Here's the source code for the example (ShowDate.vb for the Visual Basic version or ShowDate.cs for the C# version).

using System;
using System.Globalization;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguage("en")]

public class Example
{
   public static void Main()
   {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ru-RU");
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ru-RU");
      
      string[] cultureNames = { "fr-FR", "sv-SE" };
      ResourceManager rm = new ResourceManager("DateStrings",
                                               typeof(Example).Assembly);
      
      foreach (var cultureName in cultureNames) {
         CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
         string dateString = rm.GetString("DateStart", culture);
         Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString, 
                                            DateTime.Now.ToString("M", culture));                           
         Console.WriteLine();
      }   
   }
}
// The example displays output similar to the following:
//       French (France): Aujourd'hui, c'est le 7 février.
//       
//       Swedish (Sweden): Today is den 7 februari.
Imports System.Globalization
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguage("en")>

Module Example
   Public Sub Main()
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ru-RU")
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ru-RU")
      
      Dim cultureNames() As String = { "fr-FR", "sv-SE" }
      Dim rm As New ResourceManager("DateStrings",
                                    GetType(Example).Assembly)
      
      For Each cultureName In cultureNames
         Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
         Dim dateString As String = rm.GetString("DateStart", culture)
         Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString, 
                                            Date.Now.ToString("M", culture))                           
         Console.WriteLine()
      Next                                           
   End Sub
End Module
' The example displays output similar to the following:
'       French (France): Aujourd'hui, c'est le 7 février.
'       
'       Swedish (Sweden): Today is den 7 februari.

注解

桌面应用Desktop Apps

在桌面应用中, culture如果null为, GetString(String, CultureInfo)则方法CultureInfo.CurrentUICulture使用从属性获取的当前 UI 区域性。In desktop apps, if culture is null, the GetString(String, CultureInfo) method uses the current UI culture obtained from the CultureInfo.CurrentUICulture property.

返回的资源针对culture参数所指定的区域性进行了本地化。The resource that is returned is localized for the culture specified by the culture parameter. 如果尚未为culture配置资源,资源管理器会按照打包和部署资源主题的 "资源回退进程" 一节中所述的步骤来探测资源。If the resource has not been localized for culture, the resource manager probes for a resource by following the steps outlined in the "Resource Fallback Process" section of the Packaging and Deploying Resources topic. 如果未找到可用的资源集,资源管理器将回退到默认区域性的资源。If no usable set of resources is found, the resource manager falls back on the default culture's resources. 如果资源管理器无法加载默认区域性的资源集,则该方法将引发MissingManifestResourceException异常,或者,如果资源集预期位于附属程序集中MissingSatelliteAssemblyException ,则会引发异常。If the resource manager cannot load the default culture's resource set, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. 如果资源管理器可以加载相应的资源集,但找不到名name为的资源, null则该方法将返回。If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

属性确定name与资源名称的比较是否不区分大小写(默认值)或区分大小写。 IgnoreCaseThe IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

注意

此方法可能会引发比列出的更多异常。This method can throw more exceptions than are listed. 出现这种情况的原因之一是,此方法调用的方法会引发异常。One reason this might occur is if a method that this method calls throws an exception. 例如, FileLoadException如果在部署或安装附属程序集时出错,则可能会引发异常; 如果在反SerializationException序列化类型时用户定义的类型引发用户定义的异常,则可能引发异常。For example, a FileLoadException exception might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException exception might be thrown if a user-defined type throws a user-defined exception when the type is deserialized.

Windows 8.x 应用商店Windows 8.x Store 应用程序Apps

重要

尽管此ResourceManager类在应用中Windows 8.x 应用商店Windows 8.x Store受支持,但我们不建议使用它。Although the ResourceManager class is supported in Windows 8.x 应用商店Windows 8.x Store apps, we do not recommend its use. 仅当你开发可移植类库Portable Class LibraryWindows 8.x 应用商店Windows 8.x Store用于应用程序的项目时,才使用此类。Use this class only when you develop 可移植类库Portable Class Library projects that can be used with Windows 8.x 应用商店Windows 8.x Store apps. 若要从Windows 8.x 应用商店Windows 8.x Store应用中检索资源,请改用windows.applicationmodel.resources.core. windows.applicationmodel.resources.resourceloader类。To retrieve resources from Windows 8.x 应用商店Windows 8.x Store apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.

Windows 8.x 应用商店Windows 8.x Store应用程序中GetString(String, CultureInfo) ,方法name返回字符串资源的值culture ,该资源针对参数指定的区域性进行了本地化。In Windows 8.x 应用商店Windows 8.x Store apps, the GetString(String, CultureInfo) method returns the value of the name string resource, localized for the culture specified by the culture parameter. 如果没有为culture区域性本地化该资源,则查找将使用整个Windows 8Windows 8语言回退列表,并在查找默认区域性后停止。If the resource is not localized for the culture culture, the lookup uses the entire Windows 8Windows 8 language fallback list, and stops after looking in the default culture. 如果资源管理器不匹配name,则该方法null返回。If the resource manager cannot match name, the method returns null.

安全性

ReflectionPermission
当通过诸如InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])等机制后期绑定调用时。when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). 关联枚举:MemberAccessAssociated enumeration: MemberAccess.

线程安全性

方法GetString(String, CultureInfo)是线程安全的。The GetString(String, CultureInfo) method is thread safe.

另请参阅

适用于