2009/07/23

C# - Read From File and Write To File

Read from File

using System.IO;

public class FileClass

{

public static void Main()

{

ReadFromFile("c:\\MyTextFile.txt");

}

static void ReadFromFile(string filename)

{

StreamReader SR;

string S;

SR=File.OpenText(filename);

S=SR.ReadLine();

while(S!=null)

{

Console.WriteLine(S);

S=SR.ReadLine();

}

SR.Close();

}

}



Write from File

using System.IO;

public class FileClass

{

public static void Main()

{

WriteToFile();

}

static void WriteToFile()

{

StreamWriter SW;

SW=File.CreateText("c:\\MyTextFile.txt");

SW.WriteLine("God is greatest of them all");

SW.WriteLine("This is second line");

SW.Close();

Console.WriteLine("File Created SucacessFully");

}

}

No comments:

Post a Comment

Google Analytics

Blog Archive

Followers