Read a text file with LINQ in CSharp

Description

The following code shows how to read a text file with LINQ.

Example


/*from ww w.ja v a 2 s  .  com*/
using System;
using System.IO;
using System.Linq;

class Program
{
    static string dataPath = @"Main.cs";

    static void Main(string[] args)
    {

        var text = from line in File.ReadLines(dataPath)
                   where (line.Contains("a"))
                   select line;

        File.WriteAllLines(@"data.txt", text);

    }
}




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip