Export (0) Print
Expand All

Retrieving Resources with the ResourceManager Class

Visual Studio .NET 2003

You can use the ResourceManager Class to retrieve embedded resources (resources that have been compiled into an application or class library) at run time. Each instance of the ResourceManager class is associated with an assembly and manages retrieval of the resources embedded in that assembly.

To retrieve a resource

  1. Create a reference to the assembly that contains the resources you want to access. If the resource-containing assembly has not been loaded, it must be loaded at this time.
  2. Create an instance of ResourceManager class to retrieve the resources.
  3. Specify the base name of the embedded file and provide a reference to the assembly that contains the resources.
    Note   The base name of an embedded resource file is the name of the namespace that the resource file is embedded and the name of the file without any extensions. For example, the base name of a file named Resource1.resX in the myApplication namespace would be myApplication.Resource1.
  4. Call the ResourceManager.GetString Method or ResourceManager.GetObject Method to retrieve your resources.
    • To retrieve String resources, call the GetString method.
    • To retrieve resources of other types, call the GetObject method. Resources retrieved by means of the GetObject method must be explicitly converted to the appropriate type.

Example

' Visual Basic .NET
' Gets a reference to the same assembly that 
' contains the type that is creating the ResourceManager.
Dim myAssembly As System.Reflection.Assembly
myAssembly = Me.GetType.Assembly

' Gets a reference to a different assembly.
Dim myOtherAssembly As System.Reflection.Assembly
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly")

' Creates the ResourceManager.
Dim myManager As New _
   System.Resources.ResourceManager("ResourceNamespace.myResources", _
   myAssembly)

Dim myString As System.String
Dim myImage As System.Drawing.Image
myString = myManager.GetString("StringResource")
myImage = CType(myManager.GetObject("ImageResource"), _
    System.Drawing.Image)

// C#
// Gets a reference to the same assembly that 
// contains the type that is creating the ResourceManager.
System.Reflection.Assembly myAssembly;
MyAssembly = this.GetType().Assembly;

// Gets a reference to a different assembly.
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new 
   System.Resources.ResourceManager("ResourceNamespace.myResources", 
   myAssembly);

// Retrieves String and Image resources.
System.String myString;
System.Drawing.Image myImage;
myString = myManager.GetString("StringResource");
myImage = (System.Drawing.Image)myManager.GetObject("ImageResource");

See Also

Creating and Editing Resource Files with ResEditor | Embedding Resource Files in Applications | Creating Resource Assemblies | ResourceManager Class

Show:
© 2014 Microsoft