Community discussion forum

reading a text file in asp.net

  • 9 months ago
    Hi. Does anybody know the best (fastest) way to read down a text file in asp.net Current example: If I have a text file with 3000 lines in it and I want to pick out every line with the word HELP in it I can use a System.io.streamreader to read the text file into a string, use the split function to break it into an array and the loop through the array looking for the HELP string.... This, as you can imagine, is slow. Not tragic, but slow enough. I am trying to get somewhere near the performance of the PHP Perl page I am replacing which seems unfeasably good at reading text files and grepping them for pattern matches. Any ideas of how to get some better performance from asp.net here? Thanks in advance Tom
    Post was edited on 05/02/2009 10:53:26 Report abuse
  • 9 months ago
    Hi, Have you tried using a regular expression to do this? it can populate a list of matches that you can iterate over, should be quicker. Si
  • 9 months ago
    Thanks for the response. I can imagine regex being quick but not sure how to implement it over a multi line text file. Any ideas?
  • 9 months ago
    Well, you basically just read the whole file into a string using io.file.readalltext and pass in the path. assign the output to a string. use a regex to create a match collection that you can iterate over if you need the full line, make sure your regex looks from line feed to line feed. Si
  • 9 months ago
    Yup asp i always heard this.. I wonder what does it do.
  • 9 months ago
    Dim sr As System.IO.StreamReader = System.IO.File.OpenText(v_FileName) If sr Is Nothing Then Return Nothing End If Dim arrFILE(0) As String Dim sologline As String = "" Do While Not sr.EndOfStream sfileline = sr.ReadLine If InStr(sfileline, "HELP") Then If ologFILE(0) <> "" Then Array.Resize(sfileline, ologFILE.Length + 1) End If arrFILE(sfileline.Length - 1) = sologline End If Loop If arrFILE.Length = 1 Then If arrFILE(0) = "" Then 'bail Return Nothing End If End If
  • 9 months ago
    Bottom line Reading a stream line by line is WAAAAAAAAY quicker than reading a whole file and going through it line by line. Who knew.... Thanks Tom
  • 9 months ago
    really? well, will have to put that little titbit of information it the keep box. Cheers Si

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback