ResXResourceSet Class

Represents all resources in an XML resource (.resx) file.

System.Object
  System.Resources.ResourceSet
    System.Resources.ResXResourceSet

Namespace:  System.Resources
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public class ResXResourceSet : ResourceSet

The ResXResourceSet type exposes the following members.

  NameDescription
Public methodResXResourceSet(Stream)Initializes a new instance of the ResXResourceSet class using the system default ResXResourceReader to read resources from the specified stream.
Public methodResXResourceSet(String)Initializes a new instance of a ResXResourceSet class using the system default ResXResourceReader that opens and reads resources from the specified file.
Top

  NameDescription
Public methodCloseCloses and releases any resources used by this ResourceSet. (Inherited from ResourceSet.)
Public methodDispose()Disposes of the resources (other than memory) used by the current instance of ResourceSet. (Inherited from ResourceSet.)
Protected methodDispose(Boolean)Releases resources (other than memory) associated with the current instance, closing internal managed objects if requested. (Inherited from ResourceSet.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetDefaultReaderReturns the preferred resource reader class for this kind of ResXResourceSet. (Overrides ResourceSet.GetDefaultReader().)
Public methodGetDefaultWriterReturns the preferred resource writer class for this kind of ResXResourceSet. (Overrides ResourceSet.GetDefaultWriter().)
Public methodGetEnumeratorReturns an IDictionaryEnumerator that can iterate through the ResourceSet. (Inherited from ResourceSet.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObject(String)Searches for a resource object with the specified name. (Inherited from ResourceSet.)
Public methodGetObject(String, Boolean)Searches for a resource object with the specified name in a case-insensitive manner, if requested. (Inherited from ResourceSet.)
Public methodGetString(String)Searches for a String resource with the specified name. (Inherited from ResourceSet.)
Public methodGetString(String, Boolean)Searches for a String resource with the specified name in a case-insensitive manner, if requested. (Inherited from ResourceSet.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodReadResourcesReads all the resources and stores them in a Hashtable indicated in the Table property. (Inherited from ResourceSet.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Protected fieldReaderIndicates the IResourceReader used to read the resources. (Inherited from ResourceSet.)
Protected fieldTableThe Hashtable in which the resources are stored. (Inherited from ResourceSet.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an IEnumerator object to avoid a race condition with Dispose. This member is not intended to be used directly from your code. (Inherited from ResourceSet.)
Top

The ResXResourceSet class enumerates over an IResourceReader, loads every name and value, and stores them in a hash table. You can then enumerate the resources in the ResXResourceSet object or retrieve individual resources by name.

NoteNote

This class contains a SecurityAction.LinkDemand and an SecurityAction.InheritanceDemand at the class level that applies to all members. A SecurityException exception is thrown when the immediate caller or a derived class does not have full-trust permission. For more information about security demands, see Link Demands and Inheritance Demands.

A ResXResourceSet object provides a convenient way to read all the resources in a .resx file into memory. You can use the GetObject method to retrieve a particular resource when the .resx file has been read into a ResXResourceSet instance.

Notes to Inheritors

Derived classes of ResXResourceSet that use their own resource reader and writer should override the GetDefaultReader and GetDefaultWriter methods to provide the appropriate functionality for interpreting the ResXResourceSet instance.

The following example instantiates a ResXResourceSet object and illustrates how to enumerate its resources and retrieve individual resources by name. For each resource that it enumerates, the example uses the IDictionaryEnumerator.Key property in a call to the GetString or GetObject method, depending on whether the value of the resource is a string or an object.

using System;
using System.Collections;
using System.Drawing;
using System.Resources;

public class Example
{
   public static void Main()
   {
      CreateResXFile();

      ResXResourceSet resSet = new ResXResourceSet(@".\StoreResources.resx");
      IDictionaryEnumerator dict = resSet.GetEnumerator();
      while (dict.MoveNext()) {
         string key = (string) dict.Key;
         // Retrieve resource by name. 
         if (dict.Value is string)
            Console.WriteLine("{0}: {1}", key, resSet.GetString(key));
         else
            Console.WriteLine("{0}: {1}", key, resSet.GetObject(key));   
      }
   }

   private static void CreateResXFile()
   {
      Bitmap logo = new Bitmap(@".\Logo.bmp");
      ResXDataNode node;

      ResXResourceWriter rw = new ResXResourceWriter(@".\StoreResources.resx");
      node = new ResXDataNode("Logo", logo);
      node.Comment = "The corporate logo.";
      rw.AddResource(node); 
      rw.AddResource("AppTitle", "Store Locations");
      node = new ResXDataNode("nColumns", 5);
      node.Comment = "The number of columns in the Store Location table";
      rw.AddResource(node);
      rw.AddResource("City", "City");
      rw.AddResource("State", "State");
      rw.AddResource("Code", "Zip Code");
      rw.AddResource("Telephone", "Phone");
      rw.Generate();
      rw.Close();
   }
}
// The example displays the following output: 
//       Telephone: Phone 
//       Code: Zip Code 
//       State: State 
//       City: City 
//       nColumns: 5 
//       AppTitle: Store Locations 
//       Logo: System.Drawing.Bitmap

The example calls a CreateResXFile method to create the necessary XML resource file. It requires a bitmap file named Logo.bmp in the directory in which the example is running.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft