C# FileInfo OpenText

Description

FileInfo OpenText Creates a StreamReader with UTF8 encoding that reads from an existing text file.

Syntax

FileInfo.OpenText has the following syntax.


public StreamReader OpenText()

Returns

FileInfo.OpenText method returns A new StreamReader with UTF8 encoding.

Example

The following example reads text from a file.


//w w  w.  ja  v  a2 s  . c  o  m
using System;
using System.IO;
using System.Text;

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

        FileInfo fi = new FileInfo(path);
        using (StreamReader sr = fi.OpenText())
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter