Export (0) Print
Expand All

ResourceManager.GetString Method (String)

Returns the value of the specified string resource.

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)

public virtual string GetString(
	string name
)

Parameters

name
Type: System.String
The name of the resource to retrieve.

Return Value

Type: System.String
The value of the resource localized for the caller's current UI culture, or null if name cannot be found in a resource set.

ExceptionCondition
ArgumentNullException

The name parameter is null.

InvalidOperationException

The value of the specified resource is not a string.

MissingManifestResourceException

No usable set of resources has been found, and there are no resources for the default culture. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

MissingSatelliteAssemblyException

The default culture's resources reside in a satellite assembly that could not be found. For information about how to handle this exception, see the "Handling MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions" section in the ResourceManager class topic.

The resource that is returned is localized for the UI culture of the current thread, as defined by the 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, which are usually expected to be in the main assembly. If the resource manager cannot load the default culture's resource set, the method throws a MissingManifestResourceException exception. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null.

The IgnoreCase property determines whether the comparison of name with the names of resources is case-insensitive (the default) or case-sensitive.

Caution noteCaution

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. 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.

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

The following source code file is named ShowDate.vb or ShowDate.cs.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft