ResourceReader 构造函数

定义

初始化 ResourceReader 类的新实例。

重载

ResourceReader(Stream)

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

ResourceReader(String)

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

注解

> [!重要提示] > 将此对象的实例与不受信任的数据结合使用存在安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅 验证所有输入

ResourceReader(Stream)

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 参数不可读。

stream 参数为 null

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

示例

本部分中的示例使用以下名为 PatientForm.txt .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:"

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

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) 构造函数。

重要

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

另请参阅

适用于

ResourceReader(String)

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 不区分大小写。

例外

fileName 参数为 null

找不到该文件。

发生了 I/O 错误。

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

示例

本部分中的示例使用以下名为 PatientForm.txt .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) 构造函数。

重要

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

适用于