System.Resources.ResourceManager.GetString methods
This article provides supplementary remarks to the reference documentation for this API.
The IgnoreCase property determines whether the comparison of name
with the names of resources is case-insensitive (the default) or case-sensitive.
Note
The GetString methods can throw more exceptions than are listed. One reason this might occur is if a method that this method calls throws an exception. 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.
GetString(String) method
Desktop apps
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. 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. 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. If the resource manager can load an appropriate resource set but cannot find a resource named name
, the method returns null
.
Windows 8.x apps
Important
Although the ResourceManager class is supported in Windows 8.x apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows 8.x apps. To retrieve resources from Windows 8.x apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.
In Windows 8.x apps, the GetString(String) method returns the value of the name
string resource, localized for the caller's current UI culture settings. The list of cultures is derived from the operating system's preferred UI language list. If the resource manager cannot match name
, the method returns null
.
Example
The following example uses the GetString method to retrieve culture-specific resources. It consists of resources compiled from .txt files for the English (en), French (France) (fr-FR), and Russian (Russia) (ru-RU) cultures. The example changes the current culture and current UI culture to English (United States), French (France), Russian (Russia), and Swedish (Sweden). It then calls the GetString method to retrieve the localized string, which it displays along with the current day and month. 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. Each has a single string resource named DateStart
.
Culture | File name | Resource name | Resource value |
---|---|---|---|
en-US | DateStrings.txt | DateStart |
Today is |
fr-FR | DateStrings.fr-FR.txt | DateStart |
Aujourd'hui, c'est le |
ru-RU | DateStrings.ru-RU.txt | DateStart |
Сегодня |
You can use the following batch file to compile the C# example. For 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
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;
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.
GetString(String, CultureInfo) method
Desktop apps
In desktop apps, if culture
is null
, the GetString(String, CultureInfo) method uses the current UI culture obtained from the CultureInfo.CurrentUICulture property.
The resource that is returned is localized for the culture specified by the culture
parameter. 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. 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. If the resource manager can load an appropriate resource set but cannot find a resource named name
, the method returns null
.
Windows 8.x apps
Important
Although the ResourceManager class is supported in Windows 8.x apps, we do not recommend its use. Use this class only when you develop Portable Class Library projects that can be used with Windows 8.x apps. To retrieve resources from Windows 8.x apps, use the Windows.ApplicationModel.Resources.ResourceLoader class instead.
In Windows 8.x apps, the GetString(String, CultureInfo) method returns the value of the name
string resource, localized for the culture specified by the culture
parameter. If the resource is not localized for the culture
culture, the lookup uses the entire Windows 8 language fallback list, and stops after looking in the default culture. If the resource manager cannot match name
, the method returns null
.
Example
The following example uses the GetString(String, CultureInfo) method to retrieve culture-specific resources. 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. The example changes the current culture and current UI culture to Russian (Russia) before calling GetString(String, 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. 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. Each has a single string resource named DateStart
.
Culture | File name | Resource name | Resource value |
---|---|---|---|
en-US | DateStrings.txt | DateStart |
Today is |
fr-FR | DateStrings.fr-FR.txt | DateStart |
Aujourd'hui, c'est le |
ru-RU | DateStrings.ru-RU.txt | DateStart |
Сегодня |
You can use the following batch file to compile the Visual Basic example. 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
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;
public class Example2
{
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
Module Example2
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.