C# File WriteAllLines(String, IEnumerable<String>)

Description

Creates a new file, writes a collection of strings to the file, and then closes the file.

Syntax


public static void WriteAllLines(
  string path,
  IEnumerable<string> contents
)

Parameters

  • path - The file to write to.
  • contents - The lines to write to the file.

Example

The following example writes selected lines from a sample data file to a file.


/*from  w  ww .  j  ava2  s  . com*/
using System;
using System.IO;
using System.Linq;

class Program
{
    static string dataPath = @"c:\temp\data.txt";

    static void Main(string[] args)
  {

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

    File.WriteAllLines(@"C:\temp\new.txt", da);

  }

}




















Home »
  C# Tutorial »
    System.IO »




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