Create or open text file to write the text in c#

If you want to to create a text file and write the text and if the file already exist then just append the text in the new line using C#, you can use the following code snippet in c#.

The following example creates or open a file to write the text.

string path = @"c:\temp\MyTest.txt"; 
using (StreamWriter sw = File.CreateText(path)) 

How to open the file to read using File.OpenText() method

You can also use the following code for opening the file to read.

using (StreamReader sr = File.OpenText(path)) 
   string s = "";
   while ((s = sr.ReadLine()) != null) 

You can see above that we are reading the each line from the text file. however there are several ways to read the text file you can see this : Different methods for reading text file in C# .