There are a lot of different ways to read and write files (text files, not binary) in C#.
I just need something that is easy and uses the least amount of c
In addition to File.ReadAllText
, File.ReadAllLines
, and File.WriteAllText
(and similar helpers from File
class) shown in another answer you can use StreamWriter/StreamReader classes.
Writing a text file:
using(StreamWriter writetext = new StreamWriter("write.txt"))
{
writetext.WriteLine("writing in text file");
}
Reading a text file:
using(StreamReader readtext = new StreamReader("readme.txt"))
{
string readText = readtext.ReadLine();
}
Notes:
using
/Close
is very common reason of "why data is not written to file".