How to read an entire file to a string using C#?

Shamim Hafiz picture Shamim Hafiz · Sep 12, 2011 · Viewed 262.6k times · Source

What is the quickest way to read a text file into a string variable?

I understand it can be done in several ways, such as read individual bytes and then convert those to string. I was looking for a method with minimal coding.

Answer

marc_s picture marc_s · Sep 12, 2011

How about File.ReadAllText:

string contents = File.ReadAllText(@"C:\temp\test.txt");