Write lines to a text file in CSharp

Description

The following code shows how to write lines to a text file.

Example


//from  ww  w . j a v a2s.  co m
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class MainClass
{
    static void Main(string[] args)
    {
        string[] lines = new string[10];
        for (int i = 0; i < 10; i++)
        {
            lines[i] = String.Format( "This is line number {0}", i);
        }

        File.WriteAllLines(@"c:\test.txt", lines);

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




















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