Try
If System.IO.File.Exists(Application.StartupPath & "\UserHistory.txt") = False Then
System.IO.File.Create(Application.StartupPath & "\UserHistory.txt") 'CREATE FILE
End If 'DO I NEED TO RELEASE THE FILE OR SOMETHING??
Catch ex As Exception
MessageBox.Show(ex.Message & "ERROR PLACE 1")
End Try
'THERE'S CODE HERE TO CHECK THAT ALL FIELDS HAVE BEEN FILLED IN
'THEN...
'Once TRUE create User Account
'FIRST CREATE USER FOLDER:
'DEFINE THE USER FOLDER PATH TO CHECK/CREATE
Dim userPath As String = Application.StartupPath & "\User Accounts\" & _
txtSurname.Text.ToLower & "\" & txtCreateUsername.Text.ToLower & "\"
Try
If Not My.Computer.FileSystem.DirectoryExists(userPath) Then
'CREATE FOLDER
My.Computer.FileSystem.CreateDirectory(userPath)
ElseIf My.Computer.FileSystem.DirectoryExists(userPath) Then
'Check Sub Directory Text File
MessageBox.Show("An account with these details already exists." & _
"Please select a new username or use the account details finder to" & _
"get your existing details", "Account already exists", MessageBoxButtons.OK)
txtCreateUsername.Clear() 'Clear & reselect
txtCreateUsername.Select() 'username field
Exit Sub
End If
Catch ex As Exception
MessageBox.Show(ex.Message & "ERROR PLACE 2")
End Try
'Once check has been made, create a text file that
'will contain all the user details:
Try
'Create STREAMWRITER
Dim sw As New System.IO.StreamWriter(userPath & "\userDetails.txt")
Dim UserDOB As Date = CDate(cboDay.Text & "/" & cboMonth.Text & "/" & cboYear.Text)
sw.Write("Surname:" & txtSurname.Text.ToLower & Environment.NewLine)
sw.Write("First Name:" & txtFirstName.Text.ToLower & Environment.NewLine)
sw.Write("Occupation:" & cboOccupation.Text.ToLower & Environment.NewLine)
sw.Write("DOB:" & UserDOB & Environment.NewLine)
sw.Write("Username:" & txtCreateUsername.Text.ToLower & Environment.NewLine)
sw.Write("Password:" & txtCreatePassword.Text.ToLower & Environment.NewLine)
sw.Write("Question:" & cboPWquestion.SelectedItem.ToString & Environment.NewLine) 'SELECTED SECURITY QUESTION
sw.Write("Answer:" & txtPWAnswer.Text.ToLower & Environment.NewLine) 'ANSWER TO SECURITY QUESTION
sw.Close() 'CLOSE & DISPOSE OF STREAMWRITER
sw.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message & "ERROR PLACE 3")
End Try
'ADD CUURENT USER SURNAME TO A LOG FILE OF SURNAMES:
'BEFORE ADDING, CHECK FOR DUPLICATE
Try
'FIRSTLY, READ EXISTING NAMES FROM LOG FILE
Dim srNew As New System.IO.StreamReader(Application.StartupPath & "\UserHistory.txt")
If srNew.Peek = -1 Then Exit Try 'Check if text file has text in it
Do While srNew.Peek <> -1
arr.Add(srNew.ReadLine)
Loop
srNew.Close()
srNew.Dispose()
arr.Sort()
Catch ex As Exception
MessageBox.Show(ex.Message & "ERROR PLACE 4")
End Try
Try 'NOW CHECK AGAINST TEXT ENTERED INTO SURNAME FIELD
For Each item As String In arr
If txtSurname.Text.ToLower = item Then
MessageBox.Show("Surname already exists.")
Exit Try
End If
Next
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\UserHistory.txt", _
Environment.NewLine & txtSurname.Text.ToLower, True)
Catch ex As Exception
MessageBox.Show(ex.Message & "ERROR PLACE 5")
End Try
MessageBox.Show("Congratulations! Account created successfully! You can now log on.", "Account Created", MessageBoxButtons.OK)
Me.grPersonal.Visible = False
Me.grpPassword.Visible = False
pnlLogin.Show()
Call FillUserHistory()