4

I need use the fastest method(in my opinion it is readalltext) how to readalltext from txt files,i do not know how to correct the code:

string[] files = Directory
  .GetFiles(@"C:\Users\Wiz\Desktop\test","*.txt", SearchOption.AllDirectories);

var letter = File.ReadAllText(files);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Doe
  • 43
  • 5
  • Read msdn for [`GetFiles`](https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx) and [`ReadAllText`](https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx) parameters and return values. – Sinatr Feb 29 '16 at 12:15
  • 1
    http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line – Sinan AKYAZICI Feb 29 '16 at 12:16
  • 1
    `Directory.GetFiles` returns possibly multiple files, `File.ReadAllText(files)` reads the text of a single file. Clear? You need a loop on `files`. – Tim Schmelter Feb 29 '16 at 12:16

3 Answers3

2

File.ReadAllText is expecting the path to one file but Directory.GetFiles returns a array of files. So you have to use a loop / linq to get the text of each file

string sDir = @"C:\Users\Wiz\Desktop\test";
string[] files = Directory.GetFiles(sDir, "*.txt", SearchOption.AllDirectories);
string[] letters = files.Select(x => File.ReadAllText(x)).ToArray();
fubo
  • 44,811
  • 17
  • 103
  • 137
2

The interesting thing for me was to point out, which is the FASTEST way, and therefore i tried 3 techniques:

Overview

overview *~200 Ticks

Conclusion

Parallel Approch works best in this Case, Directory.EnumerateFiles is much faster than Directory.GetFiles (searched with pattern *.txt and Subdirectories inclued)

Code

A - GetFiles and ReadAllText

foreach (var file in Directory.GetFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories))
{
   var a = File.ReadAllText(file);
}

B - EnumerateFiles and ReadAllText

foreach (var file in Directory.EnumerateFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories))
{
   var a = File.ReadAllText(file);
}

C - Parallel Approach

var files = Directory.EnumerateFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories);
Parallel.ForEach(files,(current) => 
{
    var a = File.ReadAllText(current);
});

FEEL FREE to add ideas, thoughts, ....

CeOnSql
  • 2,615
  • 1
  • 16
  • 38
  • difficult to define the BEST method, it depends if searching files or reading files is the heavier duty in a specific case... – CeOnSql Feb 29 '16 at 14:00
  • forgot: `System.Threading.Tasks.Parallel` is available since .Net Framwork 4.0 – CeOnSql Feb 29 '16 at 14:03
  • sorry @CodeCaster , don't know what mean exactly - could you please explain what's missing in your opinion? For profiling i used this function (100 iterations): http://stackoverflow.com/questions/1047218/benchmarking-small-code-samples-in-c-can-this-implementation-be-improved – CeOnSql Feb 29 '16 at 14:27
  • one more question: if i have i one txt file word image1,2,3 and in the other txt file letters a b c etc. how should i read files, that will be like this: image1 a image1 a image1 a image2 b image2 b image2 b image3 c image3 c image3 c it means that if file with image is reading finished but is not reading finished second file, first file is starting read again in (for) until the second file is not reading until end. – Doe Feb 29 '16 at 15:13
1

You need to use a loop to read all the files as ReadAllText will read one file at a time.

foreach (var file in files)
{
    // Do the reading of your file here.
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331