Click here to Skip to main content
15,881,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

I want to know how to write the code to read the binary text file "MyText.txt" which has few line of data like this, that has two values I want to get it back by separate each values by using ";" and through or using data we get to add into datagrid view line by line, i want to know how to write that code

youtube.com/watch?v=apY6IIFJfd0;youtube.com
youtube.com/watch?v=shlPQgJhG0g;youtube.com
youtube.com/watch?v=apY6IIFJfd0;youtube.com


Result i want to get in datagridview

-------------------------------------------------
|Website URL                     | Website Name |
-------------------------------------------------
|youtube.com/watch?v=apY6IIFJfd0 | youtube.com
|youtube.com/watch?v=shlPQgJhG0g | youtube.com
|youtube.com/watch?v=apY6IIFJfd0 | youtube.com


What I have tried:

I haven't try to code yet being find the solution
Posted
Updated 12-May-21 20:07pm
v2

1 solution

Use File.ReadAllLines[^] To read the data.
Process each line into separate sections using the string.Split[^] method on teh semicolon.
Then you have a choice: either add the items to existing columns of a DGV, or create a "holding class" to hold the two values in properties, and build a collection of those to provide the DGV with a DataSource.
VB
Dim lines As String() = File.ReadAllLines("d:\Test Data\yt.txt")
Dim items As List(Of [MyClass]) = lines.[Select](Function(l) New [MyClass](l.Split(";"c))).ToList()
myDataGridView.DataSource = items


VB.NET
Public Class [MyClass]
    Public Property URL As String
    Public Property Name As String

    Public Sub New(ByVal arr As String())
        URL = arr(0)
        Name = arr(1)
    End Sub
End Class

Your choice!
 
Share this answer
 
Comments
Maciej Los 13-May-21 2:01am    
5ed!
Mr.Kim2050 13-May-21 8:09am    
hi OriginalGriff

thanks, with your reply I will try the code you send to me soon but i have some more question relate to this code below which i have search from internet how to read binary text file into DataGridview, i got 3 kinds of code which i dont know why or where error with logic, please help to edit or correct the mistake for me if you know the answer why? thanks in advance

Code1:

If My.Computer.FileSystem.FileExists(MyFileName) Then
' Need to write load text file
Dim ReadStream As FileStream
Dim AcceptValue As String

Try
' Here is the error part
ReadStream = New FileStream(MyFileName, FileMode.Open)
Dim ReadBinaryVal As New BinaryReader(ReadStream)
AcceptValue = ReadBinaryVal.ReadString()
Dim Row As String() = New String() {"Model 1", AcceptValue, "Online"}
DataGridView1.Rows.Add(Row)
ReadStream.Close()
Else
Try
TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
txtStatuse.Text = "Statuse : " & MyFileName & " is created."
TextBinaryWrite.Close()
Catch ex As Exception
txtStatuse.Text = ex.Message + "Cannot create file."
Return
End Try
End If
Mr.Kim2050 13-May-21 8:10am    
Error code 2:


If My.Computer.FileSystem.FileExists(MyFileName) Then
Dim Br As BinaryReader
Dim AcceptValue As String

Try
Using BinReader As New BinaryReader(File.Open(MyFileName, FileMode.Open))
Dim Pos As Integer = 0
Dim Length As Integer = BinReader.BaseStream.Length

While Pos < Length
Dim AcceptValue As String = BinReader.ReadString
Dim Row As String() = New String() {Pos, AcceptValue, "Online"}
DataGridView1.Rows.Add(Row)
Pos += 1
End While
End Using

Else
Try
TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
txtStatuse.Text = "Statuse : " & MyFileName & " is created."
TextBinaryWrite.Close()
Catch ex As Exception
txtStatuse.Text = ex.Message + "Cannot create file."
Return
End Try
End If
Mr.Kim2050 13-May-21 8:10am    
Error code 3:

Try
Br = New BinaryReader(New FileStream(MyFileName, FileMode.Open))
AcceptValue = Br.ReadString()
Dim Row As String() = New String() {"Pos1", AcceptValue, "Online"}
DataGridView1.Rows.Add(Row)
Catch ex As Exception
txtStatuse.Text = ex.Message & "Error Load File"
Return
End Try
Br.Close()
Else
Try
TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
txtStatuse.Text = "Statuse : " & MyFileName & " is created."
TextBinaryWrite.Close()
Catch ex As Exception
txtStatuse.Text = ex.Message + "Cannot create file."
Return
End Try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900