gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
C# streamwriter to write a text file
- User
C# streamwriter to write a text file
- Assistant
To write a text file using C#'s StreamWriter, you can follow the code example below:
csharpusing System; using System.IO; class Program { static void Main() { // File path string filePath = "C:\\example.txt"; try { // Create a new instance of StreamWriter using (StreamWriter writer = new StreamWriter(filePath)) { // Write content to the text file writer.WriteLine("Hello, World!"); writer.WriteLine("This is an example."); } Console.WriteLine("Text file created successfully."); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } }