StreamReader.CurrentEncoding 属性

获取当前 StreamReader 对象正在使用的当前字符编码。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable ReadOnly Property CurrentEncoding As Encoding
用法
Dim instance As StreamReader
Dim value As Encoding

value = instance.CurrentEncoding
public virtual Encoding CurrentEncoding { get; }
public:
virtual property Encoding^ CurrentEncoding {
    Encoding^ get ();
}
/** @property */
public Encoding get_CurrentEncoding ()
public function get CurrentEncoding () : Encoding

属性值

当前读取器所使用的当前字符编码。第一次调用 StreamReader 的任何 Read 方法后,该值可能会不同,因为直到第一次调用 Read 方法时,才会进行编码的自动检测。

备注

有关此属性的用法示例,请参见示例部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

获取文件大小。

FileInfo.Length

获取文件属性。

File.GetAttributes

设置文件属性。

File.SetAttributes

确定文件是否存在。

File.Exists

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

示例

下面的代码示例获取指定的 StreamReader 对象的编码。

Imports System
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
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());
        }
    }
}
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 );
   }
}
import System.*;
import System.IO.*;
import System.Text.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";

        try {
            if (File.Exists(path)) {
                File.Delete(path);
            }
            //Use an encoding other than the default (UTF8).
            StreamWriter sw = new StreamWriter(path, false, 
                new UnicodeEncoding());
            try {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }
            finally {
                sw.Dispose();
            }
            StreamReader sr = new 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.get_CurrentEncoding());
                Console.WriteLine();
            }
            finally {
                sr.Dispose();
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

StreamReader 类
StreamReader 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本