tags:

views:

1028

answers:

6

I am realatively new to C#. I need to open bunch of code files under some directory and get particular lines out that contains some matching string. Its a simple problem, I can open files one by one using stream reader and then parsing them line by line. I was wondering if there is more efficient way of doing the same. Like I am under the impression that Stream reader and line by line read would be heavy operation.

A: 

If you need to examine the entire contents of a file, then you are going to need to read every line. ReadLine() is as good a method as any.

You can read the entire file contents at once, using StreamReader.ReadToEnd()

Mitch Wheat
A: 

The StreamReader has a Method ReadToEnd() in case you want to read all the content of a file.

Jhonny D. Cano -Leftware-
+1  A: 

File.ReadAllLines() will give you an array containing each line in the file. This may be more work though if you are able to stop reading halfway through a file. If not, it might save you some time in IO (less individual IO calls, this is just a guess though).

If you are really worried, use a profiler or write a benchmark. Otherwise, use whatever method is easiest to read.

Jamie Penney
A: 

ReadToEnd() method is indeed efficient in terms of LoC (lines of code), but if you're concern about the performance, you might want to be careful with it since it basically loads the whole file contents to the memory (string). If the file size is quite big, you'll definitely get a hit on the performance.

Ferry
A: 

Thanks for the comments. Can I use LINQ and would that make it more efficient?

MvsW
No. If you are concerned with efficiency, use Jared's answer. LINQ is not appropriate for querying txt files and would be order of magnitude less efficient since you would have to read all the files into memory and data structures before you could even use LINQ to query it.
Chad Grant
+3  A: 

There are a couple of good posts already on how to get the lines of the file so I thought I would add a bit about efficiency. A couple of people have mentioned the File.ReadAllLines() method. This method is problematic from an efficiency standpoint because it will read the entire file into memory at one time. Additionally it uses an array as storage which requires contiguous memory. If the file is sufficiently large enough this will cause problems.

A more efficient way to read the files is to use the StreamReader.ReadLine method repeatedly. It will return the lines one at a time and you only need to keep the lines you care about in memory. It's also relatively easy to turn this into a delay evaluated iterator.

public static IEnumerable<string> ReadLinesEnumerable(string path) {
  using ( var reader = new StreamReader(path) ) {
    var line = reader.ReadLine();
    while ( line != null ) {
      yield return line;
      line = reader.ReadLine();
    }
  }
}

In terms of LINQ. You can use LINQ to match on both the ReadAllLines and the ReadLinesEnumerable method equally since both return an enumerable data type. For instance

var query = from line in ReadLinesEnumerable(@"c:\some\path\file.txt")
            where Regex.IsMatch(line, @"^(\d)+.*$")
            select line;
JaredPar
This is an excellent way of doing it.
Jamie Penney