Pregunta

I have saved written a text file and it currently reads:

"first","surname","pass"

I want to read the password column so the 3rd one and define that as a variable. Its basically for a login, if pass in text file matches the entered pass (from user).

I have searched for about an hour now and no luck. Could someone guide me to a correct path.

Thanks.

¿Fue útil?

Solución

Simple example of reading a small file line by line and splitting each one into fields:

    ' get the values from the user somehow:
    Dim first As String = "James"
    Dim surname As String = "Bond"
    Dim pass As String = "007"

    Dim validated As Boolean = False ' assume wrong until proven otherwise

    ' check the file:
    Dim fileName As String = "c:\some folder\path\somefile.txt"
    Dim lines As New List(Of String)(System.IO.File.ReadAllLines(fileName))
    For Each line As String In lines
        Dim values() As String = line.Split(",")
        If values.Length = 3 Then
            If values(0).Trim(Chr(34)) = first AndAlso
                values(1).Trim(Chr(34)) = surname AndAlso
                    values(2).Trim(Chr(34)) = pass Then
                validated = True
                Exit For
            End If
        End If
    Next

    ' check the result
    If validated Then
        MessageBox.Show("Login Successful!")
    Else
        MessageBox.Show("Login Failed!")
    End If

Otros consejos

If this is a CSV file, as seems to be the case, then the easiest way to read it will be with the TextFieldParser class. The MSDN already provides an excellent example for how to use it to read a CSV file, so I won't bother reproducing it here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top