System.IO.File.ReadLines Method .net 4.0

Posted on 2012-08-10
Last Modified: 2013-01-23

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!

Question by:tutakhenaten
    LVL 96

    Accepted Solution

    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 Comment

    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() %>
    LVL 19

    Expert Comment

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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now