web analytics

Reading from and Writing to text file in C#

Options

codeling 1599 - 6654
@2017-01-03 22:16:29

The following code shows how to use StreamReader and StreamWriter to read from a text file and write 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();

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com