StreamReader.CurrentEncoding Özellik

Tanım

Geçerli nesnenin kullandığı geçerli StreamReader karakter kodlamasını alır.

C#
public virtual System.Text.Encoding CurrentEncoding { get; }

Özellik Değeri

Geçerli okuyucu tarafından kullanılan geçerli karakter kodlaması. Kodlama otomatik algılama yöntemine yapılan ilk çağrıya kadar yapılmadığından, değeri herhangi Read bir Read yöntemine StreamReaderyapılan ilk çağrıdan sonra farklı olabilir.

Örnekler

Aşağıdaki kod örneği, belirtilen StreamReader nesnenin kodlamasını alır.

C#
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());
        }
    }
}

Açıklamalar

Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.