Results 1 to 3 of 3

Thread: [RESOLVED] Checks The Informations In Text File. VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    16

    Resolved [RESOLVED] Checks The Informations In Text File. VB.NET

    0


    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.

    how can i solve this problem ?

    this is the code of "SignIn" button

    Code:
     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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,490

    Re: Checks The Informations In Text File. VB.NET

    Try it this way...

    Code:
    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 strPerson As String = txtUserName.Text & ";" & txtEmail.Text & ";" & txtPassword.Text
    
        Dim lines As New List(Of String)(IO.File.ReadAllLines("data.txt"))   
        If Not lines.Any(Function(l) l.Split(";"c)(1) = strEmail) Then
            lines.Add(strPerson)
            IO.File.WriteAllLines("data.txt", lines.ToArray)
        Else
            MsgBox("Please Change Email")
        End If
    
    End If

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2021
    Posts
    16

    Resolved Re: Checks The Informations In Text File. VB.NET

    thank you very much
    it is work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width