ResourceReader 构造函数

定义

初始化 ResourceReader 类的新实例。

重载

名称 说明
ResourceReader(Stream)

初始化指定流的类的新实例 ResourceReader

ResourceReader(String)

为指定的命名资源文件初始化类的新实例 ResourceReader

注解

Important

将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入

ResourceReader(Stream)

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

初始化指定流的类的新实例 ResourceReader

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)

参数

stream
Stream

用于读取资源的输入流。

属性

例外

参数 stream 不可读。

参数 streamnull.

访问 stream时发生 I/O 错误。

示例

本节中的示例使用以下 .txt 文件来 PatientForm.txt 定义应用程序使用的资源。

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:"

可以通过发出以下命令将 .txt 文件编译为 .resources 文件:

resgen PatientForm.txt

以下示例假定资源文件嵌入到包含应用程序的可执行代码的程序集中。 它检索从当前正在执行的程序集中命名 PatientForm.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:"

如果命名 Example.cs了 C# 示例,则可以使用以下命令对其进行编译:

csc Example.cs /res:PatientForm.resources

如果Visual Basic示例命名为 Example.vb,则可以使用以下命令对其进行编译:

vbc Example.vb /res:PatientForm.resources

注解

ResourceReader(Stream)构造函数实例化一个ResourceReader对象,该对象从独立 .resources 文件或嵌入程序集中的 .resources 文件检索资源。 若要从独立 .resources 文件读取,请实例化对象 Stream 并将其传递给 ResourceReader(Stream) 构造函数。 若要从嵌入的 .resources 文件中读取,请使用 .resources 文件的区分大小写的名称调用 Assembly.GetManifestResourceStream 该方法,并将返回 Stream 的对象传递给 ResourceReader(Stream) 构造函数。

Important

将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入

另请参阅

适用于

ResourceReader(String)

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

为指定的命名资源文件初始化类的新实例 ResourceReader

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

参数

fileName
String

要读取的资源文件的路径和名称。 filename 不区分大小写。

例外

参数 fileNamenull.

找不到该文件。

发生 I/O 错误。

资源文件的格式无效。 例如,文件的长度可能为零。

示例

本节中的示例使用以下 .txt 文件来 PatientForm.txt 定义应用程序使用的资源。

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:"

可以通过发出以下命令将此 .txt 文件编译为 .resources 文件:

resgen PatientForm.txt

以下示例枚举其中 PatientForm.resources 的资源,并显示每个资源的名称和值。

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:"

注解

构造 ResourceReader(String) 函数实例化一个 ResourceReader 对象,该对象从独立的 .resources 文件检索资源。 若要从嵌入的 .resources 文件中检索资源,请使用 ResourceReader(Stream) 构造函数。

Important

将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入

适用于