ResourceReader 建構函式

定義

初始化 ResourceReader 類別的新執行個體。

多載

ResourceReader(Stream)

為指定的資料流,初始化 ResourceReader 類別的新執行個體。

ResourceReader(String)

為指定的具名資源檔初始化 ResourceReader 類別的新執行個體。

備註

> [!重要] > 搭配不受信任的數據使用此對象的實例是安全性風險。 使用此物件時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

ResourceReader(Stream)

來源:
ResourceReader.cs
來源:
ResourceReader.cs
來源:
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 錯誤。

範例

本節中的範例會使用下列 .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:"

如果 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)

來源:
ResourceReader.cs
來源:
ResourceReader.cs
來源:
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 錯誤。

資源檔的格式無效。 例如,檔案的長度可為零。

範例

本節中的範例會使用下列 .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) 構函式。

重要

使用此物件的執行個體時,若並用了不信任的資料,會造成安全性上的風險。 使用此物件時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

適用於