Seq. Access File

Having trouble

Page 1 of 1

8 Replies - 1870 Views - Last Post: 17 March 2007 - 10:53 AM Rate Topic: -----

#1 km22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-March 07

Seq. Access File

Posted 15 March 2007 - 12:14 PM

Hi, hoping some one can help with this -
I am trying to determine whether the names.txt file exists and if it does, the procedure should show the MessageBox.show method to ask if they want to replace the existing file. Well, I keep getting the dialog box that the file doesnt exist - but it does exist. Also, I am not sure if I have the procedure in the correct order, the ' write name to the txt file code is the part where I am not sure where this should be placed. I need to be able to write two names to the names.txt file.

I am including a copy of my code, in case anybody can help with this.
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 & "names.txt") Then
			text = My.Computer.FileSystem.ReadAllText(file & "names.txt")

		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


Is This A Good Question/Topic? 0
  • +

Replies To: Seq. Access File

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Seq. Access File

Posted 15 March 2007 - 12:49 PM

You have specified the path and the filename already in your string. Also remove the last backslash at the end of the string, it is not needed.
Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt"


So you do not need to include the filename when checking if it exists. There are a couple of places where you need to make this change.
If My.Computer.FileSystem.FileExists(file) Then


Can you explain exactly what you want to do in this procedure?

Then I will better be able to tell you if things are in the correct order.
Was This Post Helpful? 0
  • +
  • -

#3 km22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-March 07

Re: Seq. Access File

Posted 15 March 2007 - 07:30 PM

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

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Seq. Access File

Posted 15 March 2007 - 07:51 PM

Topics merged.



Same problem here as before. You already have the name of the file in the string so you don't need to include it in the IF statement or in the WriteAllText.
If My.Computer.FileSystem.FileExists(file) Then
button = MessageBox.Show("Erase the file?", _
"Information", MessageBoxButtons.YesNo, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button2)
If button = DialogResult.Yes Then
My.Computer.FileSystem.WriteAllText(file, _
String.Empty, False)
End If
End If

Was This Post Helpful? 0
  • +
  • -

#5 km22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-March 07

Re: Seq. Access File

Posted 16 March 2007 - 08:47 AM

I revised my code, and I am getting the result I was trying to achieve, except that I cannot figure out how to replace a name in the names.txt file when asked by the dialog box if I want to replace the existing file. For example if the name “Laura” was in the names.txt file I answered “yes” to the dialog to replace the existing file with “Steve.” Then “Steve” should be showing up in place of “Laura.”

This is the part of the code I am referring to and am not sure how it should be changed:

If button = DialogResult.Yes Then
My.Computer.FileSystem.WriteAllText(file, _
String.Empty, False)
End If
End If

Thank you for merging my posts, I kept getting a forbidden message for replying.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Seq. Access File

Posted 16 March 2007 - 09:34 AM

When you are trying to replace, are you trying to delete all the information in the file and then insert the new information?

Or are you just trying to replace one name out of the many listed in the text file without overwriting the other names?
Was This Post Helpful? 0
  • +
  • -

#7 km22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-March 07

Re: Seq. Access File

Posted 16 March 2007 - 10:33 AM

Thank you for your response. I am trying to do exactly what you mentioned in the first paragraph - I am trying to replace all the information, and then insert the new information.
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Seq. Access File

Posted 16 March 2007 - 10:53 AM

You do not need to wipe the file clean, so your use of String.Empty is not necessary.

By setting the last parameter to False in the WriteAllText method you are telling the method that you do NOT want to append the information. So what happens is it automatically wipes the file clean and overwrites it with the new information.
		 My.Computer.FileSystem.WriteAllText(file, _
			String.Empty, False)


So all you need to do is replace String.Empty with the new information that you want to write to the file.
			If button = DialogResult.Yes Then
				My.Computer.FileSystem.WriteAllText(file, _
				name, False)
			End If


If you were to set the third parameter to True as you did earlier in the event. Then you are just adding new data to the file without deleting the old data.
Was This Post Helpful? 0
  • +
  • -

#9 km22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-March 07

Re: Seq. Access File

Posted 17 March 2007 - 10:53 AM

Thank you, I figured it was something in the String.Empty, False. It makes sense now. Here is my completed code: (in case someone else is looking to accomplish something similar..)
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) Then
			button = MessageBox.Show("Erase the file?", _
			"Information", MessageBoxButtons.YesNo, _
			MessageBoxIcon.Exclamation, _
			MessageBoxDefaultButton.Button2)
			If button = DialogResult.Yes Then
				My.Computer.FileSystem.WriteAllText(file, _
				name, False)
			End If
		End If

	End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1