ResourceManager.GetString Method (String)
Returns the value of the specified String resource.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
- Type: System.String
The name of the resource to get.
Return Value
Type: System.StringThe value of the resource localized for the caller's current culture settings. If a match is not possible, null is returned.
| Exception | Condition |
|---|---|
| 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 neutral culture resources. |
The resource that is returned is localized for the culture determined by the cultural settings of the current Thread (this is accomplished using the current thread's CurrentUICulture property). If the resource has not been localized for that culture, the resource that is returned is localized for a best match (this is accomplished using the Parent property). Otherwise, null is returned.
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.
For more information about how resources are found, see "Resource Fallback Process" in Packaging and Deploying Resources.
Note
|
|---|
|
The GetString method is thread-safe. |
Caution
|
|---|
|
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 might be thrown if an error was made deploying or installing a satellite assembly, or a SerializationException might be thrown if a user-defined type throws a user-defined exception when the type is deserialized. |
The following code example gets a string resource using the current UI culture.
using System; using System.Globalization; using System.Threading; using System.Resources; using System.Reflection; class ResourcesExample { public static void Main() { // Create a resource manager to retrieve resources. ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly()); // Retrieve the value of the string resource named "welcome". // The resource manager will retrieve the value of the // localized resource using the caller's current culture setting. String str = rm.GetString("welcome"); Console.WriteLine(str); } }
-
ReflectionPermission
when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.
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.
I use Resource files to develop MultiLingual applications.
I use GetString (string ...) to read the values in the resource file, my problem is I need to always hardcode a string for GetString functions.
I do not find this approach good, as typos could occur and are visible only in runtime... Also if the name of the string in the resource file is changed then I get runtime errors.
So Is there a way to read the name from the Resource file and its value (Value is read using GetString).
Getting Both the Name and Value of a Resource
You can enumerate the resources in a binary .resources file (either a standalone .resources file or one embedded in an assembly) by using the ResourceReader class. After you instantiate the ResourceReader instance, the ResourceReader.GetEnumerator method can be used to retrieve an enumerable IDictionaryEnumerator object. For each object returned by the collection, the IDictionaryEnumerator.Name property contains the name of the resource, and the IDictionaryEnumerator.Value property contains its value. The C# code to retrieve the resources from a .resources file embedded in the current assembly might look something like the following:
var assem = Assembly.GetExecutingAssembly();
var fs = assem.GetManifestResourceStream("PatientForm.resources");
var rr = new ResourceReader(fs);
IDictionaryEnumerator dict = rr.GetEnumerator();
while (dict.MoveNext()) {
Console.WriteLine("{0} = {1}", dict.Key, dict.Value);
}
rr.Close();
I hope that this helps.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
- 5/13/2011
- MV81
- 5/16/2011
- R Petrusha - MSFT
Note
Caution