1,105,221 Community Members

VBNet read from text file

Member Avatar
mariozeph
Newbie Poster
4 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
HctiMitcH
Newbie Poster
16 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
Maligui
Junior Poster in Training
98 posts since Aug 2012
Reputation Points: 1 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 4 [?]
 
0
 

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

Member Avatar
HctiMitcH
Newbie Poster
16 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
mariozeph
Newbie Poster
4 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

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
Member Avatar
mariozeph
Newbie Poster
4 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
HctiMitcH
Newbie Poster
16 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
mariozeph
Newbie Poster
4 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Question Answered as of 1 Year Ago by HctiMitcH, tinstaafl and Maligui
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: