ResourceReader Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy ResourceReader.

Przeciążenia

ResourceReader(Stream)

Inicjuje ResourceReader nowe wystąpienie klasy dla określonego strumienia.

ResourceReader(String)

Inicjuje ResourceReader nowe wystąpienie klasy dla określonego nazwanego pliku zasobów.

Uwagi

> [!IMPORTANT] > Użycie wystąpienia tego obiektu z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Użyj tego obiektu tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych)..

ResourceReader(Stream)

Inicjuje ResourceReader nowe wystąpienie klasy dla określonego strumienia.

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)

Parametry

stream
Stream

Strumień wejściowy do odczytywania zasobów.

Atrybuty

Wyjątki

Parametr stream nie jest czytelny.

Parametr stream ma wartość null.

Wystąpił błąd we/wy podczas uzyskiwania dostępu do elementu stream.

Przykłady

W przykładzie w tej sekcji użyto następującego pliku .txt o nazwie PatientForm.txt w celu zdefiniowania zasobów używanych przez aplikację.

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

Plik .txt można skompilować do pliku resources, wydając następujące polecenie:

PatientForm.txtponownego generowania

W poniższym przykładzie przyjęto założenie, że plik zasobu jest osadzony w zestawie zawierającym kod wykonywalny aplikacji. Pobiera plik zasobów o nazwie PatientForm.resources z aktualnie wykonywanych zestawów i wyświetla nazwę i wartość każdego z jego zasobów.

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

Jeśli przykład w języku C# ma nazwę Example.cs, możesz go skompilować przy użyciu następującego polecenia:

csc Example.cs /res:PatientForm.resources

Jeśli przykład Visual Basic ma nazwę Example.vb, możesz go skompilować przy użyciu następującego polecenia:

vbc Example.vb /res:PatientForm.resources

Uwagi

Konstruktor ResourceReader(Stream) tworzy ResourceReader wystąpienie obiektu, który pobiera zasoby z autonomicznego pliku resources lub pliku resources osadzonego w zestawie. Aby odczytać z autonomicznego pliku resources, utwórz Stream wystąpienie obiektu i przekaż go do konstruktora ResourceReader(Stream) . Aby odczytać z osadzonego pliku resources, wywołaj metodę Assembly.GetManifestResourceStream z nazwą z uwzględnieniem wielkości liter pliku resources i przekaż zwrócony Stream obiekt do konstruktora ResourceReader(Stream) .

Ważne

Użycie wystąpienia tego obiektu z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Użyj tego obiektu tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Zobacz też

Dotyczy

ResourceReader(String)

Inicjuje ResourceReader nowe wystąpienie klasy dla określonego nazwanego pliku zasobów.

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

Parametry

fileName
String

Ścieżka i nazwa pliku zasobu do odczytania. w nazwie pliku nie jest uwzględniana wielkość liter.

Wyjątki

Parametr fileName to null.

Nie można odnaleźć pliku.

Wystąpił błąd we/wy.

Plik zasobu ma nieprawidłowy format. Na przykład długość pliku może być równa zero.

Przykłady

W przykładzie w tej sekcji użyto następującego pliku .txt o nazwie PatientForm.txt w celu zdefiniowania zasobów używanych przez aplikację.

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

Ten plik .txt można skompilować do pliku resources, wydając następujące polecenie:

PatientForm.txtresgen

Poniższy przykład wylicza zasoby w pliku PatientForm.resources i wyświetla nazwę i wartość każdego z nich.

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

Uwagi

Konstruktor ResourceReader(String) tworzy ResourceReader wystąpienie obiektu, który pobiera zasoby z autonomicznego pliku resources. Aby pobrać zasoby z osadzonego pliku resources, użyj konstruktora ResourceReader(Stream) .

Ważne

Użycie wystąpienia tego obiektu z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Użyj tego obiektu tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Dotyczy