We help IT Professionals succeed at work.

System.IO.File.ReadLines Method .net 4.0

Medium Priority
1,124 Views
Last Modified: 2013-01-23
Hi,

I have a nested code that is binding to a datasource. Container.DataItem is a list of files.

I can get all the properties from the files, System.IO.File.ReadLines is working, but I can't figure out how to read a line that has a set of characters. I only want to read one line that has  the specified characters.

<%# System.IO.File.ReadLines(Container.DataItem.ToString()).Contains("Please") %>

I know this would work, if I got the syntax right. Any help is very appreciated!

Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Contains returns a boolean.  I would suggest using something in the code-behind, like a LINQ query.

using System.IO;
using System.Linq;
...


public string FindLineContainingText(string fileName, string searchText) 
{
    string[] lineList = File.ReadLines(fileName);

    string result = lineList.Where(x => x.Contains(searchText)).FirstOrDefault();

    return result;
}

Open in new window

Author

Commented:
Thank you for helping with the syntax. This code also works, in case someone needs to use it:

<%# System.IO.File.ReadLines(Container.DataItem.ToString()).Where(x => x.Contains("Please")).FirstOrDefault() %>
CERTIFIED EXPERT

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.