how to split up a text file based on a delimiter into two rows

Discussion in 'Programming' started by blitz4u, Jul 20, 2011.

  1. #1
    ii i am tryin to split up a text file using vb.net.The text file contains username and password in the format usernameassword.What i am tryin to do is to split up the text file based on the delimiter(":") and store the username and pass in two different strings.

    here is a small snippet of code i am using

    Dim FILE_NAME As String = "uname.txt"
    Dim TextLine As String
    If System.IO.File.Exists(FILE_NAME) = True Then
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    Do While objReader.Peek() <> -1
    TextLine = TextLine & objReader.ReadLine() & vbNewLine
    Loop
    Textbox1.Text = TextLine
    Else
    MsgBox("File Does Not Exist")
    End If




    Also i want to place each username and pass one by one in the string so that i can use them with my code..



    Any Help will be highly appreciated..
     
    blitz4u, Jul 20, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    For each line you have, you can use following function to read username and password:

    
    Private Sub GrabUser(ByVal line As String, ByRef username As String, ByRef password As String)
      Dim fields As String() = line.Split(New [Char]() {":"c})
      username = fields(0)
      If UBound(fields) > 1 Then
        password = fields(1)
      End If
    End Sub
    
    Code (markup):
     
    dthoai, Jul 25, 2011 IP