Home > Mobile >  Checks The Informations In Text File. VB.NET
Checks The Informations In Text File. VB.NET

Time:11-27

I work on a project "SignInLogeIn" using Visual Basic.NET.

I save the user informations in text file.

the name of the file is "data.txt".

to create a new account in my program. you must enter the name,email,password and the program write the informations in textfile.

i use "Streamwritter" to write the informations.

when user create a new account The program checks if the email entered by the user is already in the text file that contains the users' information.

and the program checks from informations by "StreamReader". it reads the information in text file and checks.

I have the problem.

when I CREATE A new account. problem appears.

and the problem is

" An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'D:\1- Anas Files\Projects\VisualBasic.NET\SignInLogIn\SignInLogIn\SignInLogIn\bin\Debug\Data.txt' because it is being used by another process. "

I think the problem is that I used the file twice Once to write and once to read.

The error occurs in this line "Dim sw As New StreamWriter("Data.txt")".

how can i solve this problem ?

this is the code of "SignIn" button

 Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
        Dim strEmail As String = txtEmail.Text
        Dim Reg As New Regex("^\w ([-_.]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*$")
        If txtUserName.Text.Trim() = "" Or txtEmail.Text.Trim() = "" Or txtPassword.Text.Trim() = "" Then
            MsgBox("Please Enter All Input")
            If Not Reg.IsMatch(strEmail) Then
                MsgBox("Please Enter Email")
            End If
        Else

            Dim sr As New StreamReader("Data.txt")
            Dim sw As New StreamWriter("Data.txt")

            Dim strPerson As String = txtUserName.Text & ";" & txtEmail.Text & ";" & txtPassword.Text
            Dim line As String = ""
            Do

                line = sr.ReadLine()

                Dim arrData As String() = line.Split(";")
                If arrData(1) = strEmail Then
                    MsgBox("Please Change Email")
                Else

                    sw.WriteLine(strPerson)
                    sw.Close()
                End If
            Loop While line <> Nothing
            sr.Close()

        End If

    End Sub

CodePudding user response:

You open twice the same file. First, to read and second to write, this is why you cannot write.

        Dim sr As New StreamReader("Data.txt")
    Dim lines As String = sr.ReadToEnd().Split(Environment.NewLine)
    sr.Close()
    Dim strPerson As String = txtUserName.Text & ";" & txtEmail.Text & ";" & txtPassword.Text
    Dim sw As New StreamWriter("Data.txt")
    For Each line As String In lines
        Dim arrData As String() = line.Split(";")
        If arrData(1) = strEmail Then
            MsgBox("Please Change Email")
            Exit For
        Else
            sw.WriteLine(strPerson)
            Exit For
        End If
    Next
    sw.Close()

CodePudding user response:

Streams need to be closed and disposed. They are usually put in Using blocks.

I wasn't quite sure of the program flow you wanted. It seemed, since you created a writer and a reader you intended to add to user to the file if they were not listed.

I broke out some of the code into separate methods. I used System.IO since we have a simple text file.

Private Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
    If ValidInput() Then
        Dim strPerson As String = $"{txtUserName.Text};{txtEmail.Text};{txtPassword.Text}"
        If Not IsUserInFile(strPerson) Then
            File.AppendAllText("Data.txt", strPerson & Environment.NewLine)
        End If
    End If
End Sub

Private Function ValidInput() As Boolean
    Dim strEmail As String = txtEmail.Text
    Dim Reg As New Regex("^\w ([-_.]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*$")
    If txtUserName.Text.Trim() = "" OrElse txtEmail.Text.Trim() = "" OrElse txtPassword.Text.Trim() = "" Then
        MsgBox("Please Enter All Input")
        Return False
        If Not Reg.IsMatch(strEmail) Then
            MsgBox("Please Enter Email")
            Return False
        End If
    End If
    Return True
End Function

Private Function IsUserInFile(Person As String) As Boolean
    Dim p = Person.Split(";"c)
    Dim lines = File.ReadAllLines("Data.txt")
    For Each line In lines
        If Person = line Then
            Return True
        End If
        Dim fields = line.Split(";"c)
        If fields(0) = p(0) AndAlso fields(2) = p(2) AndAlso fields(1) <> p(1) Then
            MessageBox.Show("Please Change Email")
            Return False
        End If
    Next
    Return False
End Function

This is going to get messy and slow if there are too many users. This info should really be in a database. The worst thing is the passwords should always be salted and hashed; never stored as plain text even is a database.

  • Related