Text File Read

In this chapter you will learn:

  1. How to read all text file content

File.ReadAllLines

using System;/*j  a v  a 2  s . co m*/
using System.Collections.Generic;
using System.Text;
using System.IO;

class MainClass
{
    static void Main(string[] args)
    {
        StreamWriter writer = new StreamWriter(@"c:\myfile.txt");
        for (int i = 0; i < 3; i++)
        {
            writer.Write(i.ToString());
        }
        writer.Flush();
        writer.Close();

        foreach (string line in File.ReadAllLines(@"c:\myfile.txt"))
        {
            Console.WriteLine(line);
        }
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use File.WriteAllLines to write content together
  2. How to write to text file line by line
Home » C# Tutorial » Stream
Stream classes
Text File Read
Text File write
Text File Create
Text File Append
Replace File Content
BinaryReader
BinaryWriter
FileStream Create
FileStream byte read and write
BufferedStream
Compare File
File Copy
File Copy with FileStream
MemoryStream
Object Serialization
String Writer