C# Program to Write to and Read from a Text File
Below is a sample C# program that writes text to a file and reads it back:
using System;
using [Link];
namespace FileReadWriteDemo
{
class Program
{
static void Main(string[] args)
{
string filePath = "[Link]"; // File name
// Writing to the file
string textToWrite = "Hello, this is a sample text written to the file!";
[Link](filePath, textToWrite);
[Link]("Text successfully written to the file.");
// Reading from the file
string readText = [Link](filePath);
[Link]("Content read from the file:");
[Link](readText);
[Link]("\nPress any key to exit...");
[Link]();
}
}
}