System.Resources.ResourceManager constructors
This article provides supplementary remarks to the reference documentation for this API.
ResourceManager(Type) constructor
This section pertains to the ResourceManager(Type) constructor overload.
Desktop apps
In desktop apps, the resource manager uses the resourceSource
parameter to load a particular resource file as follows:
- If the NeutralResourcesLanguageAttribute attribute is not used to indicate that the resources of the default culture reside in a satellite assembly, the resource manager assumes that the resource file for the default culture is found in the same assembly as the type specified by the
resourceSource
parameter. - The resource manager assumes that the default resource file has the same base name as the type specified by the
resourceSource
parameter. - The resource manager uses the default ResourceSet class to manipulate the resource file.
For example, given a type named MyCompany.MyProduct.MyType
, the resource manager looks for a .resources file named MyCompany.MyProduct.MyType.resources in the assembly that defines MyType
.
In Visual Studio, the Resource Designer automatically generates code that defines an internal
(in C#) or Friend
(in Visual Basic) class whose name is the same as the base name of the .resources file for the default culture. This makes it possible to instantiate a ResourceManager object and couple it with a particular set of resources by getting a type object whose name corresponds to the name of the resource, because as long as the class is visible to the compiler, the resources must be as well. For example, if a .resources file is named Resource1, the following statement instantiates a ResourceManager object to manage the .resources file named Resource1:
ResourceManager rm = new ResourceManager(typeof(Resource1));
If you're not using Visual Studio, you can create a class with no members whose namespace and name are the same as that of the default .resources file. The example provides an illustration.
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, ResourceManager uses the resourceSource
parameter to infer the assembly, base name, and the namespace where the resource items can be located within the app's package resource index (PRI) file. For example, given a type named MyCompany.MyProduct.MyType
that's defined in MyAssembly
, the resource manager looks for a resource set identifier named MyAssembly
and looks for a scope MyCompany.MyProduct.MyType
within that resource set. The resource manager searches for resource items under the default context (current culture, current high contrast setting, and so on) within this scope.
Example
The following example uses the ResourceManager(Type) constructor to instantiate a ResourceManager object. 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(String) method to retrieve the localized string, which displays a greeting that depends on the time of day.
The example requires three text-based resource files, as listed in the following table. Each file includes string resources named Morning
, Afternoon
, and Evening
.
Culture | File name | Resource name | Resource value |
---|---|---|---|
en-US | GreetingResources.txt | Morning |
Good morning |
en-US | GreetingResources.txt | Afternoon |
Good afternoon |
en-US | GreetingResources.txt | Evening |
Good evening |
fr-FR | GreetingResources.fr-FR.txt | Morning |
Bonjour |
fr-FR | GreetingResources.fr-FR.txt | Afternoon |
Bonjour |
fr-FR | GreetingResources.fr-FR.txt | Evening |
Bonsoir |
ru-RU | GreetingResources.ru-RU.txt | Morning |
Доброе утро |
ru-RU | GreetingResources.ru-RU.txt | Afternoon |
Добрый день |
ru-RU | GreetingResources.ru-RU.txt | Evening |
Добрый вечер |
You can use the following batch file to compile the Visual Basic example and create an executable named Greet.exe. To compile with C#, change the compiler name from vbc
to csc
and the file extension from .vb
to .cs
.
resgen GreetingResources.txt
vbc Greet.vb /resource: GreetingResources.resources
md fr-FR
resgen GreetingResources.fr-FR.txt
al /out:fr-FR\Greet.resources.dll /culture:fr-FR /embed: GreetingResources.fr-FR.resources
md ru-RU
resgen GreetingResources.ru-RU.txt
al /out:ru-RU\Greet.resources.dll /culture:ru-RU /embed: GreetingResources.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 of the code).
using System;
using System.Resources;
using System.Globalization;
using System.Threading;
[assembly: NeutralResourcesLanguage("en")]
public class Example2
{
public static void Main()
{
string[] cultureNames = [ "en-US", "fr-FR", "ru-RU", "sv-SE" ];
DateTime noon = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 12, 0, 0);
DateTime evening = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 18, 0, 0);
ResourceManager rm = new ResourceManager(typeof(GreetingResources));
foreach (var cultureName in cultureNames)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine("The current UI culture is {0}",
CultureInfo.CurrentUICulture.Name);
if (DateTime.Now < noon)
Console.WriteLine("{0}!", rm.GetString("Morning"));
else if (DateTime.Now < evening)
Console.WriteLine("{0}!", rm.GetString("Afternoon"));
else
Console.WriteLine("{0}!", rm.GetString("Evening"));
Console.WriteLine();
}
}
internal class GreetingResources
{
}
}
// The example displays output like the following:
// The current UI culture is en-US
// Good afternoon!
//
// The current UI culture is fr-FR
// Bonjour!
//
// The current UI culture is ru-RU
// Добрый день!
//
// The current UI culture is sv-SE
// Good afternoon!
Imports System.Resources
Imports System.Globalization
Imports System.Threading
<Assembly:NeutralResourcesLanguage("en")>
Module Example
Public Sub Main()
Dim cultureNames() As String = {"en-US", "fr-FR", "ru-RU", "sv-SE" }
Dim noon As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 12,0,0)
Dim evening As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 18, 0, 0)
Dim rm As New ResourceManager(GetType(GreetingResources))
For Each cultureName In cultureNames
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine("The current UI culture is {0}",
CultureInfo.CurrentUICulture.Name)
If Date.Now < noon Then
Console.WriteLine("{0}!", rm.GetString("Morning"))
ElseIf Date.Now < evening Then
Console.WriteLine("{0}!", rm.GetString("Afternoon"))
Else
Console.WriteLine("{0}!", rm.GetString("Evening"))
End If
Console.WriteLine()
Next
End Sub
End Module
Friend Class GreetingResources
End Class
' The example displays output like the following:
' The current UI culture is en-US
' Good afternoon!
'
' The current UI culture is fr-FR
' Bonjour!
'
' The current UI culture is ru-RU
' Добрый день!
'
' The current UI culture is sv-SE
' Good afternoon!
In addition to defining an app class named Example
, the source code defines an internal class whose name, GreetingResources
, is the same as the base name of the resource files. This makes it possible to successfully instantiate a ResourceManager object by calling the ResourceManager(Type) constructor.
Notice that the output displays the appropriate localized string except when the current UI culture is Swedish (Sweden), in which case it uses English language resources. Because Swedish language resources are unavailable, the app uses the resources of the default culture, as defined by the NeutralResourcesLanguageAttribute attribute, instead.
ResourceManager(String, Assembly) constructor
This section pertains to the ResourceManager(String, Assembly) constructor overload.
Desktop apps
In desktop apps, the individual culture-specific resource files should be contained in satellite assemblies, and the default culture's resource file should be contained in the main assembly. A satellite assembly is assumed to contain resources for a single culture specified in that assembly's manifest, and is loaded as necessary.
Note
To retrieve resources from .resources files directly instead of retrieving them from assemblies, you must call the CreateFileBasedResourceManager method instead to instantiate a ResourceManager object.
If the resource file identified by baseName
cannot be found in assembly
, the method instantiates a ResourceManager object, but the attempt to retrieve a specific resource throws an exception, typically MissingManifestResourceException. For information about diagnosing the cause of the exception, see the "Handling the MissingManifestResourceException Exception" section of the ResourceManager class topic.
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 resource manager uses the simple name of the assembly
parameter to look up a matching resource set in the app's package resource index (PRI) file. The baseName
parameter is used to look up a resource item within the resource set. For example, the root name for PortableLibrary1.Resource1.de-DE.resources is PortableLibrary1.Resource1.
Example
The following example uses a simple non-localized "Hello World" app to illustrate the ResourceManager(String, Assembly) constructor. The contents of a text file named ExampleResources.txt is Greeting=Hello
. When the app is compiled, the resource is embedded in the main app assembly.
The text file can be converted to a binary resource file by using the Resource File Generator (ResGen.exe) at the command prompt as follows:
resgen ExampleResources.txt
The following example provides the executable code that instantiates a ResourceManager object, prompts the user to enter a name, and displays a greeting.
using System;
using System.Reflection;
using System.Resources;
public class Example1
{
public static void Main()
{
// Retrieve the resource.
ResourceManager rm = new ResourceManager("ExampleResources",
typeof(Example).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
// The example produces output similar to the following:
// Enter your name: John
// Hello John!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Module Example1
Public Sub Main()
' Retrieve the resource.
Dim rm As New ResourceManager("ExampleResources",
GetType(Example).Assembly)
Dim greeting As String = rm.GetString("Greeting")
Console.Write("Enter your name: ")
Dim name As String = Console.ReadLine()
Console.WriteLine("{0} {1}!", greeting, name)
End Sub
End Module
' The example produces output similar to the following:
' Enter your name: John
' Hello John!
It can be compiled by using the following command in C#:
csc Example.cs /resource:ExampleResources.resources
The example retrieves a reference to the assembly that contains the resource file by passing a type defined in that assembly to the typeof
function (in C#) or the GetType
function (in Visual Basic) and retrieving the value of its Type.Assembly property.