Read and Write Text in a file in C#

The WriteAllText creates a new file, writes the contents to the file and then closes the file. If a file already exists, then this method overwrites it.
The ReadAllText opens a text file, reads all lines of the file into a string, and then closes the file.

string fileName = @"C:\Temp\Mahesh.txt";          
if
(!File.Exists(fileName))
{
    string startText = "Here is a file content" + Environment.NewLine;
    File.WriteAllText(fileName, startText);
    File.WriteAllText(fileName, "Add more content \n");
}

string
appendText = "New appened text" + Environment.NewLine;
File
.AppendAllText(fileName, appendText);
string
data = File.ReadAllText(fileName);
Console
.WriteLine(data)

Up Next
    Ebook Download
    View all
    Learn
    View all