By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

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

P: 1
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
Expand|Select|Wrap|Line Numbers
  1.   Dim fileName As String = "C:\text.txt"
  2.  
  3.   Dim lines As String() = IO.File.ReadAllLines(fileName)  ' Here I am trying to read only last 7 lines not entire file
  4.  
  5.   MsgBox(lines)
  6.  
  7.   Dts.Variables("email_txt").Value = lines
Oct 6 '14 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
When you use the IO.File.ReadAllLines 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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1.   Dim sb As New StringBuilder
  2.  
  3.   If lines.Count >= 7 Then
  4.     Dim linePosition As Integer= lines.Count - 1 
  5.     Dim numLinesRead As Integer = 0
  6.     While numLinesRead < 7
  7.      sb.AppendLine(lines(linePosition))
  8.      linePosition -= 1
  9.      numLinesRead += 1
  10.     Loop
  11.   End If
Loop forwards through your array starting 7 positions back from the end and add the line to the StringBuilder.


Expand|Select|Wrap|Line Numbers
  1.   Dim sb As New StringBuilder
  2.  
  3.   If lines.Count >= 7 Then
  4.     Dim linePosition as Integer =  lines.Count - 7 
  5.  
  6.     For linePosition To lines.Count - 1
  7.      sb.AppendLine(lines(linePosition))
  8.     Next
  9.   End If
Please note that none of the above code is tested ;)

-Frinny
Oct 6 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.