1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Visual Basic .NET' started by jaggs, Jul 20, 2011.

  1. jaggs

    jaggs Registered Member

    Joined:
    Jun 4, 2009
    Messages:
    69
    Likes Received:
    8
    Hii i am tryin to split up a text file using vb.net.The text file contains username and password in the format username:password.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..
     
  2. andee

    andee Regular Member

    Joined:
    Jul 24, 2010
    Messages:
    218
    Likes Received:
    83
    Code:
            Dim source As String = TextBox1.Text.Trim
            Dim stringItems() As String = source.Split(":")
            username = stringItems(0)
            password = stringItems(1)
    
    textbox1 contains user: pass
     
  3. itsmei

    itsmei Newbie

    Joined:
    Jul 30, 2011
    Messages:
    2
    Likes Received:
    0
    Nice, I made one of these a while back to help me split up Usernames and Passwords. If anyone wants just give me a PM :)
     
  4. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    This is super fast way to load accounts into a Collection for easy retrieve when you need them. Notice how I read the entire text file in as well as split it per line in the same instance and dump to a array, all in one command, instead of looping through the entire file with the .peek method.

    I also show how you can then pull those accounts from the Collection in the DisplayUserPass Sub. Basically if you need one at a time simply increment i for the next one...and so on and so forth. Only splitting the User : Pass Combo at the moment I need it


    Code:
        Dim colUserNames As New Collection
    
        Private Sub LoadAccounts()
            Dim FILE_NAME As String = "uname.txt"
    
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Dim txtContentAll As String() = objReader.ReadToEnd.Split(vbCrLf)
    
                For i = 0 To txtContentAll.Length
                    If txtContentAll(i) <> Nothing Then
                        colUserNames.Add(txtContentAll(i))
                    End If
                Next
            Else
                MsgBox("File Does Not Exist")
            End If
        End Sub
    
        Private Sub DisplayUserPass()
            For i = 1 To colUserNames.Count
                Dim AccountDetails As String() = colUserNames.Item(i).ToString.Split(":")
                TextBox1.AppendText("Username: " & AccountDetails(0) & " Password: " & AccountDetails(1))
            Next
        End Sub
    
     
    • Thanks Thanks x 1