Re: Help with reading text
vb.net Code:
'Get all the lines from the file.
Dim lines As String() = IO.File.ReadAllLines("file path here")
'Discard all but the last four lines.
lines = lines.Skip(lines.Length - 4).ToArray()
For Each line As String In lines
'Split the line on the pipes.
Dim fields As String() = line.Split(","c)
'Get the User Name field.
Dim userNameField As String = fields.Single(Function(f) f.TrimStart().StartsWith("User Name"))
'Split the field into name and value.
Dim fieldParts As String() = userNameField.Split(":"c)
'Get the field value.
Dim userName As String = fieldParts(1).Trim()
MessageBox.Show(userName, "User Name")
Next
Re: Help with reading text
Thanks mate I'll implement it now. ILY
Re: Help with reading text
Another quick question, don't suppose you know how to make the text box automatically scroll to the bottom when it opens the text document?
EDIT: On another thought, I don't think the above code will work. I need each of the last 4 rows seperated into 4 different variables so I can extract data from them, not all 4 rows as one chunk of text.
Thanks again.
Re: Help with reading text
You should try the code rather than assume that it won't work. File.ReadAllLines returns a String array, so you DO have individual lines.
Re: Help with reading text
Did try it, couldn't get it working. Had a few errors.
Re: Help with reading text
By the way, to put the caret at the end of the text:
vb.net Code:
myTextBox.StartPosition = myTextBox.TextLength
You may have to call ScrollToCaret afterwards too.
Re: Help with reading text
Quote:
Originally Posted by
Chaori
Did try it, couldn't get it working. Had a few errors.
I typed that code without an IDE so it might not be perfect. If you don;t tell us what the errors are, or even that there were errors, we won't be able to help you fix them. Note that that code uses LINQ, so it requires .NET 3.5 too.
Re: Help with reading text
"Sequence contains no matching element" on the line:
Dim userNameField As String = fields.Single(Function(f) f.TrimStart().StartsWith("User Name"))
Re: Help with reading text
In that case you have at least one record that doesn't have a field named "User Name" in it. I suggest that you check your data file. You can also have a look in the debugger at exactly what 'fields' does contain at that point. The code I wrote should handle the data as you described it. If the data is not as you described then you'll need to adjust the code accordingly.
Re: Help with reading text
Ahh yeah, there is a blank row at the end of every text file, that'd be why.