Still having trouble with the Seq. access file. I apologize for starting a new thread for the same topic, but I could not reply to the original post for some reason.
Now I am getting the dialog box that asks the user if they want to replace the existing file. Which was what I was trying to accomplish when the “writeButton” is clicked. (jayman9 Thank you for helping me to learn what I was doing wrong with this, now the file is showing that it exists)
Although when I answer “yes” on the dialog box to erase the existing file – it is not erasing.
What I am trying accomplish is:
1) When the writeButtons click event procedure is processed for the first time, it should be determined whether the names.txt file exists
2) If the file does exist, the user should be able to click on the yes button on the message box to replace the existing file, otherwise append to the existing file.
Test the application by:
using the “yes” button on the dialog box (this is still not working, when I do click yes to erase the files, the files do not erase)
end the application. Then type another name in the nameTextBox, click the writeButton. The application should ask if you want to replace the existing file. Click No.
end the application, use the file menu to open the names.txt file. The file should contain 2 names.
Start application again. Type another name in the nameTextBox, click the writeButton.
Click the Yes button to replace the existing file. Then the file should contain 1 name.
Revised code:CODE
Private Sub writeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles writeButton.Click
Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt"
Dim name As String
Dim text As String
Dim button As DialogResult
' write name file to txt file
name = nameTextBox.Text
My.Computer.FileSystem.WriteAllText(file, _
name, True)
My.Computer.FileSystem.WriteAllText(file, ControlChars.NewLine & ControlChars.NewLine, True)
' check for names.txt file
If My.Computer.FileSystem.FileExists(file) Then
text = My.Computer.FileSystem.ReadAllText(file)
Else
MessageBox.Show("File does not exist", "Names", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If My.Computer.FileSystem.FileExists(file & “names.txt”) Then
button = MessageBox.Show("Erase the file?", _
"Information", MessageBoxButtons.YesNo, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button2)
If button = DialogResult.Yes Then
My.Computer.FileSystem.WriteAllText(file & "names.txt", _
String.Empty, False)
End If
End If
End Sub