Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Hi! I got a little problem:
I made an application in Visual Studio 2012 and I need the application to read a "log.log" file and write the content of the file in a ListBox. I've made that using this code:
Code:
var = My.Computer.FileSystem.ReadAllText(TextBox15.Text & "\logs\log.log")
As you can see, the content of the file "log.log" is written in a variable called "var", thats okay but after that, I need the application to write the content of "var" in a LISTBOX in MULTIPLE ITEMS. The file "log.log" has multiple lines but when I write the content of the "log.log" in the Listbox, it just creates 1 item with all the content.
What I need is something that detects the different lines of the "log.log" content and write each line in a different item of the listbox.
NOTE: When I open the "log.log" file with the Windows txt editor, it displays all the content in 1 line too. If I open the "log.log" file with a program such as Notepad ++ (Code editor that supports many formats) its shows correctly, in different lines.
NOTE2: Each line of the "log.log" has 2 initial characters "ch". That may help detecting the lines and separate them.
Help please? Thanks so much.
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Hi,
The trick here is to read the file line by line or split the complete text into different lines using the Carriage Return / Line Feed character. Here is one example for you:-
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myReader As New StreamReader("d:\temp\MyFileText.txt")
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
ListBox1.Items.AddRange(myVar)
End Using
End Sub
Hope that helps.
Cheers,
Ian
If the above does not work with your file then you may need to expand on this comment a bit more:-
Quote:
NOTE2: Each line of the "log.log" has 2 initial characters "ch". That may help detecting the lines and separate them.
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Quote:
Originally Posted by
IanRyder
Hi,
The trick here is to read the file line by line or split the complete text into different lines using the Carriage Return / Line Feed character. Here is one example for you:-
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myReader As New StreamReader("d:\temp\MyFileText.txt")
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
ListBox1.Items.AddRange(myVar)
End Using
End Sub
Hope that helps.
Cheers,
Ian
If the above does not work with your file then you may need to expand on this comment a bit more:-
Hi, thanks for answering. There is an error: The type "StreamReader" is not defined.
Here is the full class:
Code:
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
'BUSCAR CARPETA CON NOMBRE VARIABLE DENTRO DE LA RUTA DE LA CARPETA PHPFREECHAT DEFINIDA EN "INICIO", CARPETA DEL LOG
For Each carpetaencontrada As String In My.Computer.FileSystem.GetDirectories _
(TextBox4.Text & "\data\private\logs")
TextBox15.Text = carpetaencontrada
Next
'LEER CONTENIDO DE FICHERO Y BUSCAR FICHERO DE LOG
Dim var As String
var = My.Computer.FileSystem.ReadAllText(TextBox15.Text & "\logs\log.log")
Using myReader As New StreamReader(TextBox15.Text & "\logs\log.log")
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
ListBox1.Items.AddRange(myVar)
End Using
End Sub
End Class
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Quote:
Originally Posted by
IanRyder
Hi,
The trick here is to read the file line by line or split the complete text into different lines using the Carriage Return / Line Feed character. Here is one example for you:-
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myReader As New StreamReader("d:\temp\MyFileText.txt")
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
ListBox1.Items.AddRange(myVar)
End Using
End Sub
Hope that helps.
Cheers,
Ian
If the above does not work with your file then you may need to expand on this comment a bit more:-
Hi, thanks for answering. There is an error: The type "StreamReader" is not defined.
Here is the full class:
Code:
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
'BUSCAR CARPETA CON NOMBRE VARIABLE DENTRO DE LA RUTA DE LA CARPETA PHPFREECHAT DEFINIDA EN "INICIO", CARPETA DEL LOG
For Each carpetaencontrada As String In My.Computer.FileSystem.GetDirectories _
(TextBox4.Text & "\data\private\logs")
TextBox15.Text = carpetaencontrada
Next
'LEER CONTENIDO DE FICHERO Y BUSCAR FICHERO DE LOG
Dim var As String
var = My.Computer.FileSystem.ReadAllText(TextBox15.Text & "\logs\log.log")
Using myReader As New StreamReader(TextBox15.Text & "\logs\log.log")
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
ListBox1.Items.AddRange(myVar)
End Using
End Sub
End Class
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Hi,
Sorry, should have said that you need to add the following Imports statement:-
Cheers,
Ian
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Quote:
Originally Posted by
IanRyder
Hi,
Sorry, should have said that you need to add the following Imports statement:-
Cheers,
Ian
Yeah, I tried with IO.StreamReader and it alows me to run the debug, but it still adds a single item with all the content of the log.log.
LOG.LOG CONTENT
Code:
ch_My room 18/02/2013 09:30:16 Manuelele ow yea
ch_My room 18/02/2013 10:08:58 guest615 joins My room
ch_My room 18/02/2013 10:09:36 guest615 hola
ch_My room 18/02/2013 10:09:49 guest615
ch_My room 18/02/2013 10:09:56 guest615
ch_My room 18/02/2013 10:09:56 guest615
ch_My room 18/02/2013 10:09:57 guest615
ch_My room 18/02/2013 10:09:57 guest615
ch_My room 18/02/2013 10:09:57 guest615
ch_My room 18/02/2013 10:09:57 guest615
ch_My room 18/02/2013 10:09:57 guest615
ch_My room 18/02/2013 10:09:58 guest615
ch_My room 18/02/2013 10:10:04 guest615 :)
ch_My room 18/02/2013 10:10:07 guest615 :drizzle:
ch_My room 18/02/2013 10:10:09 guest615 :cloudly:
ch_My room 18/02/2013 10:10:19 guest615 tira tira
ch_My room 18/02/2013 10:10:22 guest615 amai
ch_My room 18/02/2013 10:10:32 guest615 PS4:drizzle:
ch_My room 18/02/2013 10:10:34 guest615 :***:
ch_My room 18/02/2013 10:10:47 guest615 :!::!::!::!::!::!::!::!::!::!::!::!::!::!::!::!:
ch_My room 18/02/2013 10:11:03 guest615 >O<
ch_My room 18/02/2013 10:11:05 guest615 >O<
pv_d97ef4b626e518f84cd7de3cfa3f26ef3c5d2472_ebbf294852ddbda3d031fbc7ca8f0ddeaa695ba4 18/02/2013 10:11:26 guest615 amai
pv_d97ef4b626e518f84cd7de3cfa3f26ef3c5d2472_ebbf294852ddbda3d031fbc7ca8f0ddeaa695ba4 18/02/2013 10:11:34 guest615 :lol:
pv_d97ef4b626e518f84cd7de3cfa3f26ef3c5d2472_ebbf294852ddbda3d031fbc7ca8f0ddeaa695ba4 18/02/2013 10:12:03 guest615 guest615 quit
ch_My room 18/02/2013 10:12:53 Manuelele guest615 quit (timeout)
ch_My room 18/02/2013 10:20:16 Manuelele holo
pv_d97ef4b626e518f84cd7de3cfa3f26ef3c5d2472_ebbf294852ddbda3d031fbc7ca8f0ddeaa695ba4 18/02/2013 10:20:31 Manuelele Manuelele quit
As you can see, each line starts with "ch_"... The "pv_", ignoring the "pv_" lines.
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Quote:
Originally Posted by
IanRyder
Code:
Dim myVar() As String = myReader.ReadToEnd.Split(CChar(vbCrLf))
CChar(vbCrLf) will return vbCr as a single Char, so the above will split the string only on Carriage Returns, leaving any vbLf characters as the first character of every split line (except the first split line, of course).
However, it sounds like the lines in corei7's log files are terminated with only the LineFeed character, which is why the file's content is not being split by the above.
What you probably want to use is:
Code:
Dim myVar() As String = myReader.ReadToEnd.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries))
which should work for lines terminated in just a LineFeed, just a Carriage Return, or a combination of both Carriage Return and LineFeed.
However, there's an easier method:
Code:
ListBox1.Items.Clear() ' if needed ?
ListBox1.Items.AddRange(IO.File.ReadAllLines(TextBox15.Text & "\logs\log.log"))
Re: Visual Basic 2012 - File "log.log" to ListBox (multiple lines) Help
Quote:
Originally Posted by
Inferrd
CChar(vbCrLf) will return vbCr as a single Char, so the above will split the string only on Carriage Returns, leaving any vbLf characters as the first character of every split line (except the first split line, of course).
However, it sounds like the lines in corei7's log files are terminated with only the LineFeed character, which is why the file's content is not being split by the above.
What you probably want to use is:
Code:
Dim myVar() As String = myReader.ReadToEnd.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries))
which should work for lines terminated in just a LineFeed, just a Carriage Return, or a combination of both Carriage Return and LineFeed.
However, there's an easier method:
Code:
ListBox1.Items.Clear() ' if needed ?
ListBox1.Items.AddRange(IO.File.ReadAllLines(TextBox15.Text & "\logs\log.log"))
Awesome! It worked the way I wanted! with the easier method. Thanks so much @Inferrd! Thanks to IanRyder too for trying to help me ;)