ResourceReader 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ResourceReader 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| ResourceReader(Stream) |
初始化指定串流的類別新實例 ResourceReader 。 |
| ResourceReader(String) |
初始化指定命名資源檔案的新類別實例 ResourceReader 。 |
備註
Important
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
ResourceReader(Stream)
初始化指定串流的類別新實例 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) 建構子。
Important
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
另請參閱
適用於
ResourceReader(String)
初始化指定命名資源檔案的新類別實例 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)
Important
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。