StreamReader.CurrentEncoding プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

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

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

指定した 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からStreamReader.CurrentEncoding プロパティを検索する場合は、下記のリンクをクリックしてください。

- StreamReader.CurrentEncoding プロパティのページへのリンク