ApprenticeHacker ApprenticeHacker - 3 months ago 16
C# Question

Easiest way to read from and write to files

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 code, because I am going to be working with files a lot in my project. I only need something for

string
since all I need is to read and write
string
s.

Answer

File.ReadAllText

File.WriteAllText

Could not be simpler...

MSDN examples:

// Create a file to write to. 
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

// Open the file to read from. 
string readText = File.ReadAllText(path);
Comments