How do I read and write files in C#

How to read and write files in C#.

C#, File I/O, Reading Files, Writing Files
This tutorial demonstrates how to read and write files using C# programming language.
// Sample code for reading and writing files in C# using System; using System.IO; class Program { static void Main() { // Specify the file path string filePath = "example.txt"; // Writing to a file string textToWrite = "Hello, this is an example of writing to a file."; File.WriteAllText(filePath, textToWrite); Console.WriteLine("Successfully written to the file."); // Reading from a file string readText = File.ReadAllText(filePath); Console.WriteLine("Read from the file: " + readText); } }

C# File I/O Reading Files Writing Files