Updated: December 2010
Initializes a new instance of the ResourceManager class that looks up resources contained in files with the specified root name using the given assembly.
Assembly: mscorlib (in mscorlib.dll)
Public Sub New ( _ baseName As String, _ assembly As Assembly _ )
public ResourceManager( string baseName, Assembly assembly )
public:
ResourceManager(
String^ baseName,
Assembly^ assembly
)
new : baseName:string * assembly:Assembly -> ResourceManager
Parameters
- baseName
- Type: System.String
The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named "MyApplication.MyResource.en-US.resources" is "MyApplication.MyResource".
- assembly
- Type: System.Reflection.Assembly
The main assembly for the resources.
| Exception | Condition |
|---|---|
| ArgumentNullException |
The baseName or assembly parameter is null. |
The individual culture-specific resource files should be contained in satellite assemblies, with the default culture's resource file 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.
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 a MissingManifestResourceException.
Notes to Implementers
This constructor uses the system-provided ResourceSet implementation. To use a custom resource file format, you should derive from the ResourceSet class, override GetDefaultReader and GetDefaultWriter, and pass that type to the ResourceManager constructor. Using a custom ResourceSet can be useful for controlling resource caching policy or supporting your own resource file format, but is generally not necessary.
The following example uses a simple non-localized "Hello World" application to illustrate the ResourceManager constructor. The following shows the contents of a text file named ExampleResources.txt. When the application is compiled, the resource is embedded in the main application assembly.
Greeting=Hello
The text file can be converted to a binary resource file by using the Resource File Generator (ResGen.exe) at the command line 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.
Imports System.Globalization Imports System.Reflection Imports System.Resources Module Example Public Sub Main() ' Retrieve the resource. Dim rm As New ResourceManager("ExampleResources" , Assembly.GetExecutingAssembly()) 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!
using System; using System.Reflection; using System.Resources; public class Example { public static void Main() { // Retrieve the resource. ResourceManager rm = new ResourceManager("ExampleResources" , Assembly.GetExecutingAssembly()); 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!
It can be compiled by using the following command in Visual Basic:
vbc Example.vb /resource:ExampleResources.resources
or by using the following command in C#:
csc Example.cs /resource:ExampleResources.resources
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 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.
Reference
|
Date |
History |
Reason |
|---|---|---|
|
December 2010 |
Replaced the example. |
Customer feedback. |
|
September 2010 |
Expanded the baseName parameter description and the Remarks section. |
Customer feedback. |