here’s a sample code in C# using the .NET framework to write data to a text or flat file
using System; using System.IO; namespace TextWriterExample { class Program { static void Main(string[] args) { // Define the text file path and name string filePath = @"C:\Users\UserName\Documents\example.txt"; // Define the data to be written to the text file string data = "Hello, world!"; // Open the text file for writing using (StreamWriter sw = new StreamWriter(filePath, true)) { // Write the data to the text file sw.WriteLine(data); } Console.WriteLine("Data has been written to the text file."); Console.ReadLine(); } } }
In this example, the StreamWriter
class is used to write the data to the text file. The using
statement ensures that the StreamWriter
object is disposed of properly when it is no longer needed. The WriteLine
method is used to write the data to the text file, followed by a new line character to move the cursor to the next line.