Read last 7 lines from text box and store into string variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mpate123
    New Member
    • Oct 2014
    • 1

    Read last 7 lines from text box and store into string variables

    Hi, I am try to read last seven line from text file and try to stored into string variable but receiving error. I am not sure what error but something wrong in my logic. please help if you can.

    Thanks.

    here is my code
    Code:
      Dim fileName As String = "C:\text.txt"
    
      Dim lines As String() = IO.File.ReadAllLines(fileName)  ' Here I am trying to read only last 7 lines not entire file
           
      MsgBox(lines)
    
      Dts.Variables("email_txt").Value = lines
    Last edited by Frinavale; Oct 6 '14, 03:33 PM. Reason: Added code tags and formatted code so that it is easier to read
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    When you use the IO.File.ReadAll Lines method it returns an array of strings that are the lines in the file.

    You are attempting to display an array of strings in the message box...but I think this will simply display something like System.Array and probably isn't crashing anything.

    You are also attempting to do this:
    Code:
    Dts.Variables("email_txt").Value = lines
    Which is probably where you are having a problem. I am going assume that Dts.Variables(" email_txt") is a TextBox....and I doubt that a TextBox has an Value property which is probably your issue.

    You should change your code so that it knows that the type is a TextBox you can set it's Text Property.

    Like this:
    Code:
    DirectCast(Dts.Variables("email_txt"), TextBox).Text = lines
    However, the above code will only display the first line in the array in the TextBox and you want to display the last 7.

    Keeping in mind that the TextBox can only display 1 string, you will need to create a string with the appropriate content for it to display.

    I recommend that you use the http://msdn.microsoft.com/en-us/libr...gBuilder Class to help you with this.

    Loop through your array backwards from the end 7 positions and add the line to the StringBuilder.

    Code:
      Dim sb As New StringBuilder
     
      If lines.Count >= 7 Then
        Dim linePosition As Integer= lines.Count - 1 
        Dim numLinesRead As Integer = 0
        While numLinesRead < 7
         sb.AppendLine(lines(linePosition))
         linePosition -= 1
         numLinesRead += 1
        Loop
      End If
    Loop forwards through your array starting 7 positions back from the end and add the line to the StringBuilder.


    Code:
      Dim sb As New StringBuilder
     
      If lines.Count >= 7 Then
        Dim linePosition as Integer =  lines.Count - 7 
    
        For linePosition To lines.Count - 1
         sb.AppendLine(lines(linePosition))
        Next
      End If
    Please note that none of the above code is tested ;)

    -Frinny

    Comment

    Working...