password text file

Timbo292832

Member
Joined
Apr 7, 2013
Messages
5
Location
Nsw Australia
Programming Experience
Beginner
hi guys
im having trouble having my program read the password from a text file and compare it to the one in a textbox

the plan is for the program to compare the password in the textbox to the one in the text file

writing the password to the file is easy enough
i have spent hours on this and i know its simple but im just not seeing it
could someone please give me an example on how to do this as im brain is incapable of figuring this out lol at the moment
 
Hi,

writing the password to the file is easy enough
i have spent hours on this and i know its simple but im just not seeing it

If you can write the file easy enough then reading the file is just as easy. Have a look at the StreamReader class:-

StreamReader Class (System.IO)

If the only thing in the file is the password then you can use the ReadToEnd method otherwise you can use the ReadLine method to read each line of the file and then compare each line to the value of your TextBox.

Hope that helps.

Cheers,

Ian
 
hi guys
im having trouble having my program read the password from a text file and compare it to the one in a textbox

the plan is for the program to compare the password in the textbox to the one in the text file

writing the password to the file is easy enough
i have spent hours on this and i know its simple but im just not seeing it
could someone please give me an example on how to do this as im brain is incapable of figuring this out lol at the moment
Could you by chance post the code you have?

The simplest way to read a text file is to use the System.IO.StreamReader:
'Very top of code file:
Imports System.IO

Public Class Form1

 Private Function ReadFile(ByVal FileName As String) As Boolean
        Dim Success As Boolean = False 'Assume the worst

        Dim sr As StreamReader = Nothing
        Try
            sr = New StreamReader(FileName)

            'To read a single line:
            Dim strLine As String = sr.ReadLine

            'To read multiple lines:
            Dim arrStr As New List(Of String)
            While sr.Peek <> -1I
                arrStr.Add(sr.ReadLine)
            End While

            Success = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "ReadFile")
        Finally
            If sr IsNot Nothing Then
                'Cleanup reader object 
                sr.Close()
                sr.Dispose()
            End If
        End Try

        Return Success
    End Function

End Class
And to write to a file:
'Very top of code file:
Imports System.IO

Public Class Form1

   Private Function WriteFile(ByVal FileName As String, ByVal Contents As String) As Boolean
        Dim Success As Boolean = False 'Assume the worst

        Dim sw As StreamWriter = Nothing
        Try
            sw = New StreamWriter(FileName, False)
            sw.Write(Contents)

            Success = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "WriteFile")
        Finally
            If sw IsNot Nothing Then
                'Cleanup writer object 
                sw.Close()
                sw.Dispose()
            End If
        End Try

        Return Success
    End Function

End Class
 
Back
Top