using System.IO; FileStream fileStream = new FileStream("example.txt", FileMode.Create); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.Write("This is an example text"); streamWriter.Flush(); // Flushes the data to the file streamWriter.Close(); fileStream.Close();
using System.IO; FileStream fileStream = new FileStream("example.txt", FileMode.Append); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine("This is another example line."); streamWriter.Flush(); // Flushes the data to the file streamWriter.Close(); fileStream.Close();In this example, we create a FileStream object with the specified file name and FileMode.Append to add new text to the file. We again create a StreamWriter object from it, write a new line to the file, and flush the data to ensure it's saved. Finally, we close both the StreamWriter and FileStream objects. The System.IO FileStream and StreamWriter classes belong to the .NET Framework Class Library.
public Flush ( ) : void | ||
return | void |