Export (0) Print
Expand All

ResourceReader Constructor (Stream)

Updated: June 2011

Initializes a new instance of the ResourceReader class for the specified stream.

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)

public ResourceReader(
	Stream stream
)

Parameters

stream
Type: System.IO.Stream
The input stream for reading resources.

ExceptionCondition
ArgumentException

The stream is not readable.

ArgumentNullException

The stream parameter is null.

IOException

An I/O error has occurred while accessing stream.

The ResourceReader constructor instantiates a ResourceReader object that retrieves resources either from a standalone .resources file or from a .resources file that is embedded in an assembly. To read from a standalone .resources file, instantiate a Stream object and pass it to the ResourceReader constructor. To read from an embedded .resources file, call the Assembly.GetManifestResourceStream method with the case-sensitive name of the .resources file, and pass the returned Stream object to the ResourceReader constructor.

The example in this section uses the following .txt file named PatientForm.txt to define the resources used by an application.


Title="Top Pet Animal Clinic"
Label1="Patient Number:"
Label2="Pet Name:"
Label3="Species:"
Label4="Breed:"
Label5="Date of Birth:"
Label6="Age:"
Label7="Owner:"
Label8="Address:"
Label9="Home Phone:"
Label10="Work Phone:"
Label11="Mobile Phone:"

You can compile the .txt file into a .resources file by issuing the following command:

resgen PatientForm.txt

The following example assumes that the resource file is embedded in the assembly that contains the application's executable code. It retrieves a resource file named PatientForm.resources from the currently executing assemblies and displays the name and value of each of its resources.


using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Resources;

public class Example
{
   public static void Main()
   {
      var assem = Assembly.GetExecutingAssembly();
      var fs = assem.GetManifestResourceStream("PatientForm.resources");
      var rr = new ResourceReader(fs);
      IDictionaryEnumerator dict = rr.GetEnumerator();
      int ctr = 0;

      while (dict.MoveNext()) {
         ctr++;
         Console.WriteLine("{0:00}: {1} = {2}", ctr, dict.Key, dict.Value);
      }
      rr.Close();
   }
}
// The example displays the following output:
//       01: Label3 = "Species:"
//       02: Label2 = "Pet Name:"
//       03: Label1 = "Patient Number:"
//       04: Label7 = "Owner:"
//       05: Label6 = "Age:"
//       06: Label5 = "Date of Birth:"
//       07: Label4 = "Breed:"
//       08: Label9 = "Home Phone:"
//       09: Label8 = "Address:"
//       10: Title = "Top Pet Animal Clinic"
//       11: Label10 = "Work Phone:"
//       12: Label11 = "Mobile Phone:"


If the C# example is named Example.cs, you can compile it by using the following command:

csc Example.cs /res:PatientForm.resources

If the Visual Basic example is named Example.vb, you can compile it by using the following command:

vbc Example.vb /res:PatientForm.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 SP1

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.

Date

History

Reason

June 2011

Revised extensively.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft