System.IO.File.ReadLines Method .net 4.0

Posted on 2012-08-10
Medium Priority
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

Bob Learned earned 2000 total points
ID: 38283927
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

ID: 38294514
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
ID: 38809088
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

862 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