แก้ไข

แชร์ผ่าน


ResourceReader Constructors

Definition

Initializes a new instance of the ResourceReader class.

Overloads

ResourceReader(Stream)

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

ResourceReader(String)

Initializes a new instance of the ResourceReader class for the specified named resource file.

Remarks

> [!IMPORTANT] > Using an instance of this object with untrusted data is a security risk. Use this object only with trusted data. For more information, see Validate All Inputs..

ResourceReader(Stream)

Source:
ResourceReader.cs
Source:
ResourceReader.cs
Source:
ResourceReader.cs

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

public:
 ResourceReader(System::IO::Stream ^ stream);
public ResourceReader (System.IO.Stream stream);
[System.Security.SecurityCritical]
public ResourceReader (System.IO.Stream stream);
new System.Resources.ResourceReader : System.IO.Stream -> System.Resources.ResourceReader
[<System.Security.SecurityCritical>]
new System.Resources.ResourceReader : System.IO.Stream -> System.Resources.ResourceReader
Public Sub New (stream As Stream)

Parameters

stream
Stream

The input stream for reading resources.

Attributes

Exceptions

The stream parameter is not readable.

The stream parameter is null.

An I/O error has occurred while accessing stream.

Examples

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 = typeof(Example).Assembly;
      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:"
Imports System.Collections
Imports System.IO
Imports System.Reflection
Imports System.Resources

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Example).Assembly
      Dim fs As Stream = assem.GetManifestResourceStream("PatientForm.resources")
      Dim rr As New ResourceReader(fs)
      Dim dict As IDictionaryEnumerator = rr.GetEnumerator
      Dim ctr As Integer

      Do While dict.MoveNext()
         ctr += 1
         Console.WriteLine("{0:00}: {1} = {2}", ctr, dict.Key, dict.Value)
      Loop

      rr.Close()
   End Sub
End Module
' 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

Remarks

The ResourceReader(Stream) 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(Stream) 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(Stream) constructor.

Important

Using an instance of this object with untrusted data is a security risk. Use this object only with trusted data. For more information, see Validate All Inputs.

See also

Applies to

ResourceReader(String)

Source:
ResourceReader.cs
Source:
ResourceReader.cs
Source:
ResourceReader.cs

Initializes a new instance of the ResourceReader class for the specified named resource file.

public:
 ResourceReader(System::String ^ fileName);
public ResourceReader (string fileName);
new System.Resources.ResourceReader : string -> System.Resources.ResourceReader
Public Sub New (fileName As String)

Parameters

fileName
String

The path and name of the resource file to read. filename is not case-sensitive.

Exceptions

The fileName parameter is null.

The file cannot be found.

An I/O error has occurred.

The resource file has an invalid format. For example, the length of the file may be zero.

Examples

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 this .txt file into a .resources file by issuing the following command:

resgen PatientForm.txt

The following example enumerates the resources in PatientForm.resources and displays the name and value of each.

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

public class Example
{
   public static void Main()
   {
      var rr = new ResourceReader("PatientForm.resources");
      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:"
Imports System.Collections
Imports System.Resources

Module Example
   Public Sub Main()
      Dim rr As New ResourceReader("PatientForm.resources")
      Dim dict As IDictionaryEnumerator = rr.GetEnumerator
      Dim ctr As Integer

      Do While dict.MoveNext()
         ctr += 1
         Console.WriteLine("{0:00}: {1} = {2}", ctr, dict.Key, dict.Value)
      Loop
      
      rr.Close()
   End Sub
End Module
' 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:"

Remarks

The ResourceReader(String) constructor instantiates a ResourceReader object that retrieves resources from a standalone .resources file. To retrieve resources from an embedded .resources file, use the ResourceReader(Stream) constructor.

Important

Using an instance of this object with untrusted data is a security risk. Use this object only with trusted data. For more information, see Validate All Inputs.

Applies to