次の方法で共有


StreamReader.CurrentEncoding プロパティ

現在の StreamReader が使用している現在の文字エンコーディングを取得します。

Public Overridable ReadOnly Property CurrentEncoding As Encoding
[C#]
public virtual Encoding CurrentEncoding {get;}
[C++]
public: __property virtual Encoding* get_CurrentEncoding();
[JScript]
public function get CurrentEncoding() : Encoding;

プロパティ値

現在のリーダーが使用している現在の文字エンコーディング。 StreamReaderRead メソッドを最初に呼び出した後、現在の文字エンコーディングを示す値が異なる場合があります。これは、 Read メソッドの最初の呼び出しまでエンコードの自動検出が実行されないためです。

解説

このプロパティの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] 指定した 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

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"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(S"This");
            sw->WriteLine(S"is some text");
            sw->WriteLine(S"to test");
            sw->WriteLine(S"Reading");
        } __finally {
            if (sw) __try_cast<IDisposable*>(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(S"The encoding used was {0}.", sr->CurrentEncoding);
            Console::WriteLine();
        } __finally {
            if (sr) __try_cast<IDisposable*>(sr)->Dispose();
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

StreamReader クラス | StreamReader メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み