Using WriteLine method
using System.IO;
using System;
class WriteFile
{
static void Main()
{
// Writes file with StreamWriter.
string fileLoc = "D://MyFile.txt";
FileStream fs = new FileStream(fileLoc, FileMode.Create); // Creates file with the specified path
fs.Close();
Console.WriteLine("File created successfully");
StreamWriter sw = new StreamWriter(fileLoc); // StreamWriter is used to write to the file
sw.WriteLine("word1"); // WriteLine method appends a newline character.
sw.WriteLine("word2");
sw.WriteLine("word3");
sw.Flush();
Console.WriteLine("Written to file successfully");
sw.Close();
Console.ReadLine();
}
}
Output:
File created successfully
Written to file successfully