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

jaggs

Registered Member
Joined
Jun 4, 2009
Messages
71
Reaction score
10
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..
 
Code:
        Dim source As String = TextBox1.Text.Trim
        Dim stringItems() As String = source.Split(":")
        username = stringItems(0)
        password = stringItems(1)

textbox1 contains user: pass
 
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 :)
 
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
 
Back
Top