Jul 01
Here is the easiest way to read an entire text file into a C# string:
string s = System.IO.File.ReadAllText( path );
Here is the easiest way to read an entire text file into a C# string:
string s = System.IO.File.ReadAllText( path );
It is interesting to notice that the File.ReadAllText and File.ReadAllLines methods were added in the version 2.0 of the .NET framework; mainly because in the previous versions, the necessary usage of a Stream object made reading text files difficult and unintuitive for most of the developers.
thanx
How do u read a file like blablabla.txt and say if on it theres like 1001 u download a file
How to read a RTF File from embedded resource into a C# string?
thanks–that helps me a lot!
Good, what about case when I need to use the text line by line. Should I split it after that? or do I need to read it into array
I know this question is answered already, but here’s a blog which benchmarks 9 common techniques to determine the fastest way.
Definitely worth a look for those interested in the fastest way:
http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-read-text-files
_
If you have a lot of processing to do within the file itself, the fastest way is to read the entire file contents into an array, close the file, then loop over the array using a Parallel.For loop.
Benchmarks and sample code here to prove it’s the fastest way to go:
http://blogs.davelozinski.com/curiousconsultant/the-fastest-way-to-read-and-process-text-files
Updated Reference links above are:
http://cc.davelozinski.com/c-sharp/fastest-way-to-read-text-files
and
http://cc.davelozinski.com/c-sharp/the-fastest-way-to-read-and-process-text-files
respectively.