Hi Ladies and Gents,
Please can you help with the below
The SaveFileDialog is appending to my text files and I can't find a way to get it to overwrite whatever existing text is in the save file.
Please can someone advise me
Private
Sub SaveToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriterSave.Filter =
"Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"Save.CheckPathExists =
TrueSave.Title =
"Save"Save.ShowDialog(
Me) TrymyStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
Catch ex As Exception ' Do nothing on Exception End Try End Sub
Help with SaveFileDialog???
cran7465
Dim myStreamReader As System.IO.StreamReader
Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
Open.CheckFileExists =
TrueOpen.Title =
"Open"Open.ShowDialog(
Me) TryOpen.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
Catch ex As Exception End Try' you need to explictly close the stream here
myStreamReader.Close() 'This should fix it
End Sub
Give that a try and see if it doesn't work for you.
It works fine here.
deadeye
Below is the source code.
I use another Function called SaveChanges which determines if the user wishes to save their changes before opening, closing, exiting or creating a new text file. Based on the users response either the sub continues or it executes the SaveFile function before continuing.
As I stated earlier, there is only a problem if I open an existing text file and try to save over it.
I can run the SaveFile function over the same file multiple times without causing a problem, it's only if I try to save the same file that I have currently open.
So, as I have already determined, the fault lies somewhere within the OpenFile code. By adding the myStreamReader.Close code in the last version of this program, I was able to stop this from happening, but in this instance it gives the same problem. In all logical processes, the code is executing in a logical process and while debugging, the myStreamReader.Close code is executing too, meaning that for all intents and purposes, the file should register as closed and allow deletion.
I'm really stumped by this one as it worked without a problem before, and still does. It seems that somewhere within the IF..ELSE statements a problem lies that I have not considered.
Any help with this would be greatly appreciated!
(The OpenFile parameter that is passed to the SaveChanges function is just a simple text string added to a messagebox. I thought the problem might lie here as there may be a conflict between this and the actual OpenFile name, but after changing the string label and testing, I discovered that it isn't the problem.)
************************************************
Public Function SaveFile() As Boolean Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriterSave.Filter =
"Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"Save.CheckPathExists =
TrueSave.Title =
"Save Text Document"Save.ShowDialog(
Me) If My.Computer.FileSystem.FileExists(Save.FileName) = True Then Try My.Computer.FileSystem.DeleteFile(Save.FileName)myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Return True Catch ex As ExceptionMsgBox(
"ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly) Return False End Try
Else
TrymyStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Return True Catch ex As ExceptionMsgBox(
"ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly) Return False End Try End If End Function************************************************
Private
Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click Dim Open As New OpenFileDialog() Dim myStreamReader As System.IO.StreamReaderOpen.Filter =
"Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"Open.CheckFileExists =
TrueOpen.Title =
"Open Text Document"Open.ShowDialog(
Me) If SaveChanges(OpenFile) = True Then TryOpen.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Close()
Catch ex As ExceptionMsgBox(
"ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly) End Try ElseSaveFile()
TryOpen.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Close()
Catch ex As ExceptionMsgBox(
"ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly) End Try End If End Sub************************************************
TheMaj0r
millsapflashATyahooDOTcom
of course you know how to fix that!
I understand about not wanting to post your entire project. Send what you wish and I will try to help you more.
james
aka:Trucker
Don Shaw
To help you track down the problem(s) you are having you need to go ahead
and proccess the Catch Statement, like so:
MsgBox("ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly)
Or you could use: Console.WriteLine(ex.Source & " : " & ex.Message).ToString
either way, you can get a better idea as to what is happening with your program.
You mentioned another thread in your post, I am by no means an expert on threading, but, I believe you need to explicitly stop the other thread first before you can access a file it may have open.
First thing I would do is as I mentioned above and insert the code I posted right after the Catch ex as Exception to see what is happening in your case.
Good luck,
james
aka:Trucker
Carmen Zlateff MSN
First if you want to overwrite your file then you'll have to see if it exists. If it exists then delete it. If not then use CreateText instead of AppendText to have a new file...
cheers,
Paul June A. Domag
Alan Hebert - MSFT
Hi Paul,
I've tried the following, but I can't get the previous thread to release/close the file. Please advise
Private Sub SaveToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
Save.CheckPathExists =
TrueSave.Title =
"Save"Save.ShowDialog(
Me) If My.Computer.FileSystem.FileExists(Save.FileName) = True Then Try ' I need to determine id another thread has this file open' If so, I need to close that thread/file so that my application can delete the file and re-create it as below:
My.Computer.FileSystem.DeleteFile(Save.FileName)myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Catch ex As Exception End Try
Else
TrymyStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Catch ex As Exception End Try End IfEnd
SubPeter R. Fletcher
I think I've tried this, but it was before I implemented the file deletion process, when I was stillt rying to get the StreamWriter to stop amending to my textfiles.

I'll give it a bash and will let you know.
I'll honestly kick myself if it's something this simple...
Aspera
If SaveChanges(OpenFile) = True Then
Without seeing the SaveChanges Function, I cannot tell what is happening I get an error if I try to run your code as-is. Also, the (OpenFile) creates an error. I changed it to SaveChanges(Open) and the error for OpenFile went away.But, the SaveChanges still throws an error.
I can set SaveChanges to True and then this works:
If SaveChanges = True then
(leaving out the (Open) or (OpenFile) part. It saves and overwrites just fine.
Do you have a button on the Toolstrip that also Opens a file for your user to work with Or are you trying to combine everything into a single button It would be easier to debug and maintain , if you seperated the Opening and Saving(or closing if there are no changes) into two buttons (or menu selections) based on whatever feedback from your user that you get to determine if they need to save changes etc.
james
aka:Trucker
luisfdlr
Hi James,
Thanks for the valueable feedback.
When catching the exception, I get the following:
ERROR: mscorlib : The process cannot access the file "C:\test.txt" because it it being used by another process.
I did notice while testing that I can save a new text file as many times as I need to without any problems. The error that I experience occurs when I copy text from a new or existing text file, open an already existing text file and paste the text into the existing file. When I try to save the amended existing text file, it gives the error.
I was advised that if I wanted to amend an existing text file, I needed to first delete the original file and then write everything to a new copy of the text file, as per the above posts.
I think the problem may be in the code block that I use to open an existing text file, perhaps once the file is opened and read to my textbox, it isn't closing the file again (or the process) and therefore when I try to delete the file from the Save code block, the system does not allow it because it is already opened by another process
If that is the case, then please advise me as to how I can kill any active VB processes regarding that file so that I can then delete the existing file I cannot find anything on this in the VB helpfiles or online searches.
Below is my code blocks for opening and saving the text files:
*****************************************
Opening
*****************************************
Private Sub OpenToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click Dim Open As New OpenFileDialog() Dim myStreamReader As System.IO.StreamReaderOpen.Filter =
"Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"Open.CheckFileExists =
TrueOpen.Title =
"Open"Open.ShowDialog(
Me) TryOpen.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
Catch ex As Exception End Try End Sub*****************************************
Saving
*****************************************
Private
Sub SaveToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriterSave.Filter =
"Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"Save.CheckPathExists =
TrueSave.Title =
"Save"Save.ShowDialog(
Me) If My.Computer.FileSystem.FileExists(Save.FileName) = True Then Try My.Computer.FileSystem.DeleteFile(Save.FileName)myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Catch ex As ExceptionMsgBox(
"ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OKOnly) End Try
Else
TrymyStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
Catch ex As Exception End Try End IfEnd
Sub*****************************************
ASharif
This did fix the problem until I decided to change the program structure.
I wrote a function to handle the SaveFileDialog for me as it makes sense to use one function instead of the same code in 4 seperate Subs.
The problem now persists when I use my Open sub...
I get the exact error message that I posted before, and I have definitely isolated the cause the the OpenDialog sub because the problem only comes into play once I've opened a file and then try to save it.
Even though I am explicitly telling the system to close the StreamReader after reading the contents of the file, for some reason a process is keeping the file open so that I cannot delete it.
I'd be grateful for any suggestions on this
EddieR
Coolbeans...
I'm not using a toolstrip at all.
I've created a menu. Say the user wishes to create a new text file.
The application determines if there has been any changes to the current text file and prompts the user with a MessageBox if they wish to save changes before creating a new text file. (This is all that the SaveChanges function determines, it returns "True" if the user wishes to save or "False" if the user wishes to continue without saving, depending on which button the user clicks on the Messagebox)
I do have seperate subs for each menu option, eg New, Open, Close, Exit, Save. However, I'd like to give the user the option of saving before creating a new, opening and exisiting, closing an existing or exiting the program.
With that in mind I wrote the SaveFile function to be called by which ever sub requires it before continuing with the selected menu option.
I'm not too comfortable posting my entire code on the forum, is there any way that I can PM it to you so that you can perhaps test it on your side
Will McAfee
james
aka:Trucker