StreamReader.CurrentEncoding Свойство

Определение

Возвращает текущую кодировку символов, используемую текущим объектом StreamReader.

public:
 virtual property System::Text::Encoding ^ CurrentEncoding { System::Text::Encoding ^ get(); };
public virtual System.Text.Encoding CurrentEncoding { get; }
member this.CurrentEncoding : System.Text.Encoding
Public Overridable ReadOnly Property CurrentEncoding As Encoding

Значение свойства

Текущая кодировка символов, используемая текущим устройством чтения. После первого вызова любого метода Read объекта StreamReader значение может измениться, поскольку до первого вызова метода Read автоматическое определение кодировки не выполняется.

Примеры

В следующем примере кода возвращается кодировка указанного StreamReader объекта .

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }

      //Use an encoding other than the default (UTF8).
      StreamWriter^ sw = gcnew StreamWriter( path,false,gcnew UnicodeEncoding );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      StreamReader^ sr = gcnew StreamReader( path,true );
      try
      {
         while ( sr->Peek() >= 0 )
         {
            Console::Write( (Char)sr->Read() );
         }

         //Test for the encoding after reading, or at least
         //after the first read.
         Console::WriteLine( "The encoding used was {0}.", sr->CurrentEncoding );
         Console::WriteLine();
      }
      finally
      {
         delete sr;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;
using System.Text;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            //Use an encoding other than the default (UTF8).
            using (StreamWriter sw = new StreamWriter(path, false, new UnicodeEncoding()))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path, true))
            {
                while (sr.Peek() >= 0)
                {
                    Console.Write((char)sr.Read());
                }

                //Test for the encoding after reading, or at least
                //after the first read.
                Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding);
                Console.WriteLine();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            'Use an encoding other than the default (UTF8).
            Dim sw As StreamWriter = New StreamWriter(path, False, New UnicodeEncoding())
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path, True)

            Do While sr.Peek() >= 0
                Console.Write(Convert.ToChar(sr.Read()))
            Loop

            'Test for the encoding after reading, or at least
            'after the first read.
            Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding)
            Console.WriteLine()

            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

Комментарии

Список распространенных задач ввода-вывода см. в разделе Общие задачи ввода-вывода.

Применяется к

См. также раздел