Read All Lines from a file with Encoding settings in CSharp

Description

The following code shows how to read All Lines from a file with Encoding settings.

Example


//from w  w  w  . j  a  v  a  2 s .  c  om
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path))
        {
            string[] createText = { "A", "B", "C" };
            File.WriteAllLines(path, createText, Encoding.UTF8);
        }
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}




















Home »
  C# Tutorial »
    Development »




Console
Encoding
Environment
Random