You can read the file line by line and close it if the value is found:
static string[] SearchFiles(string[] filesSrc, string searchTerm) { List<string> result = new List<string>(); string line = ""; StreamReader reader = null; for (int i = 0; i < filesSrc.Length; i++) { reader = new StreamReader(filesSrc[i]); while ((line = reader.ReadLine()) != null) if (line.Contains(searchTerm)) { result.Add(filesSrc[i]); break; } } reader.Dispose(); return result.ToArray(); }
And use it like: string[] files = SearchFiles(yourfiles[], "searchTerm");
Depending on what you need, you can pass File[] this method and then get the string value with the full path, but you have not provided an example of your File class, and it is difficult to implement without knowing what your class really is looks like.
PS Using LINQ is another possible and good solution (not to mention that it is just 1-2 lines of code).
Improvised showed that LINQ at this stage is 10-20% slower, so it is probably better to stick with it.
source share