C# reading from and write to text file

CSharp sample for reading from a text file and writing to a text file.

//Reading from a text file      

System.IO.StreamReader srFile = new System.IO.StreamReader(@"c:\foo.txt");

string str = srFile.ReadToEnd();

srFile.Close();

 

//Writing to a text file

string lines = "foobar";

System.IO.StreamWriter swFile = new System.IO.StreamWriter(@"c:\bar.txt");

swFile.WriteLine(lines);

swFile.Close();

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>