Hello I'm new here and have been searching for a while but haven't came up with any results that satisfies my question. Basaically I'm making a tool that is sort of an account manager and needs to read some usernames from sample text file for example

Sample.txt:
username1:password1
Username2:password2

I want these to become variables Username & Password and then loop through them, thank you.

Recommended Answers

All 8 Replies

Try This:

Dim open = OpenFileDialog1.FileName '// or you can specify the file path
        If IO.File.Exists(open) Then '// check if file exists
            Dim openFileLines() As String = IO.File.ReadAllLines(open)
            For Each line As String In openFileLines '// loop thru array list.
                Dim lineArray() As String = line.Split(":") '// separate by ":" character.
                Dim username As lineArray(0) '// add username Item before ":"
                Dim password = lineArray(1) '// add password from line
                '// What you wish to do with every username and password
            Next
        End If

This will open your .txt file and read each line. Before the 'Next' you must add your own code, for example verify that username and password = the textbox on form.

commented: Wonderful Information thanks! +0

I would also recommend encrypting at the very least the password by using using System.Cryptography.AESManaged, or similar.

Thank you very much your code works very well one last question though when i use this it only displays the first username1:password1 how can i make it show the other lines I tried username(1) and password(1) but that makes it that it onl shows 1 letter. Thanks in advanced! I have already implemented a encryption into the files

Something like this might work for you:

Dim Names As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim open = OpenFileDialog1.FileName '// or you can specify the file path
If IO.File.Exists(open) Then '// check if file exists
    Dim openFileLines() As String = IO.File.ReadAllLines(open)
    For Each line As String In openFileLines '// loop thru array list.
        Dim lineArray() As String = line.Split(":") '// separate by ":" character.
        Names.Add(lineArray(0), lineArray(1))
    Next
    'Find the name and get the password
    If Names.ContainsKey(TextBox1.Text) Then
        TextBox2.Text = Names(TextBox1.Text)
    End If
End If
commented: Thank you for this! +0

Hello to the above post I'm not quite sure I understand this part of the coding (forgive me for i am new)

 'Find the name and get the password
    If Names.ContainsKey(TextBox1.Text) Then
        TextBox2.Text = Names(TextBox1.Text)

I'm guessing this is assuming I have 2 textboxes one for username and the other for passwords which is not the case unfortunately. I was wondering if there was a way to place all of the arrays into a variable so that I can call them and use them in a couple of other functions so if I do something such as TextBox1.Text = "Current Username: " & Username(1) Would echo the username from line 1 in the label. Would this be possible? The first coded that I received does this already but it doesn't pick usernames from other lines other than one unless I just don't know how to use it

The strings 'username' and 'password' are strings, they are not arrays. The For Loop will loop for each line in the txt file. So if you would like to combine all of the usernames and passwords into one string it would look like this.

 Dim open = OpenFileDialog1.FileName '// or you can specify the file path
 Dim userlist as string
 Dim passwordlist as string
            If IO.File.Exists(open) Then '// check if file exists
                Dim openFileLines() As String = IO.File.ReadAllLines(open)
                For Each line As String In openFileLines '// loop thru array list.
                    Dim lineArray() As String = line.Split(":") '// separate by ":" character.
                    Dim username As lineArray(0) '// add username Item before ":"
                    Dim password = lineArray(1) '// add password from line
                    '// What you wish to do with every username and password
                    userlist = userlist + " " + username
                    passwordlist = passwordlist + " " + username
                    msgbox(userlist + " " + passwordlist)
                    '//This will put all of the usernames and passwords into two strings.
                Next
            End If

The msgbox will output "username1 username2...password1 password2"
If you want to put the username and passwords into an array try this:

Dim open = OpenFileDialog1.FileName '// or you can specify the file path
Dim userarray as string
Dim passwordarray as string
Dim n as integer = 0
                If IO.File.Exists(open) Then '// check if file exists
                    Dim openFileLines() As String = IO.File.ReadAllLines(open)
                    For Each line As String In openFileLines '// loop thru array list.
                        Dim lineArray() As String = line.Split(":") '// separate by ":" character.
                        Dim username As lineArray(0) '// add username Item before ":"
                        Dim password = lineArray(1) '// add password from line
                        '// What you wish to do with every username and password
                        userarray(n) = username
                        passwordarray(n) = password
                        n = n + 1
                    Next
                End If

Here useraray(n) will equal username(n) where the first username is zero.
I am new to vb.net like yourself and do not no much about FOR LOOPS and 'i', so I use my own variable 'n'. There are ways to simplify this but I don't know how. I am learning just as much as you here.

commented: Thanks for all the help! +0

Hello thank you for all your help I have found a solution I'm going to post it in case anyone is wondering I'm not sure if this is the smartest way to do this but it works me:

Dim LineSkip As Integer = 0
                If IO.File.Exists(open) Then ' check if file exists
                FileOpened = True
                Dim OpenFileLines() As String = IO.File.ReadAllLines(open)
                Dim LineCount = IO.File.ReadAllLines(open).Length
                Dim LineSkipCount = OpenFileLines(LineSkip)
                Dim lineArray() As String = LineSkipCount.Split(":") '// separate by ":" character.
                Dim Username = lineArray(0) '// add username Item before ":"
                Dim Pasword = lineArray(1)
                status.Text = "" & Username

I know it doesn't seem like there's a reason to count the lines yet but thats because I haven't implemented the rest of my code now I can update the line with my interger if I update it to be 1 it will read line 1 and so on. Thanks everyone for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.