write to text file

  • Thread starter Thread starter Verde
  • Start date Start date
V

Verde

New to C#/.NET and want to write to a text file... got lost in the
documentation... streamwriter this and textwriter that and system.io
another... Can someone provide a simple example of writing to a text file?
thanks.
 
Hi Verde,

Search for FileStream constructor or StreamWriter constructor. There
should be a list of examples, a couple of them very simple text writers.

StreamWriter sw = new StreamWriter(@"c:\mypath\myfile.txt", true);
sw.WriteLine("I just added a new line");
sw.Close();
sw.Dispose();

This creates a new file called myfile.txt (if mypath exists), and writes
"I just added a new line" to the file. If the file exists if appends the
line to the end of the file.

Happy coding!
Morten
 
Back
Top