![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
StreamWriter Problem
Hello,
Here's a very annoying problem that i couldn't make out. The problem is that: I open a text file using "streamreader" with no problem then when i save my existing txt file using streamWriter, sometimes it works very well, but sometimes it doesn't save and overwrite existing file. (Texbox1.text is main text area.) The problem doesn't occur sometimes but it really hurts. You click save button and exit. Sometimes changes have been saved successfully (re-open and see with Windows Notepad), but sometimes nothing has been saved. That's really cruel. :-( Here is streamwriter code: Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriter Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)| *.*" Save.CheckPathExists = True Save.Title = "Save" Save.ShowDialog(Me) Try myStreamWriter = System.IO.File.CreateText(Save.FileName) myStreamWriter.Write(TextBox1.Text) myStreamWriter.Flush() Catch ex As Exception ' Do nothing on Exception End Try End Sub What's wrong here? Note that, when the fake-save problem occurs, if i re-save file with several retries without closing my project, then it gets saved. Regards. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 8, 12:48 am, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
> Hello, > Here's a very annoying problem that i couldn't make out. The problem > is that: I open a text file using "streamreader" with no problem then > when i save my existing txt file using streamWriter, sometimes it > works very well, but sometimes it doesn't save and overwrite existing > file. (Texbox1.text is main text area.) > > The problem doesn't occur sometimes but it really hurts. You click > save button and exit. Sometimes changes have been saved successfully > (re-open and see with Windows Notepad), but sometimes nothing has been > saved. That's really cruel. :-( > > Here is streamwriter code: > > Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As > System.Object, ByVal e As System.EventArgs) Handles > SaveAsToolStripMenuItem.Click > Dim Save As New SaveFileDialog() > Dim myStreamWriter As System.IO.StreamWriter > > Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)| > *.*" > Save.CheckPathExists = True > Save.Title = "Save" > Save.ShowDialog(Me) > Try > myStreamWriter = System.IO.File.CreateText(Save.FileName) > myStreamWriter.Write(TextBox1.Text) > myStreamWriter.Flush() > Catch ex As Exception > ' Do nothing on Exception > > End Try > > End Sub > > What's wrong here? Note that, when the fake-save problem occurs, if i > re-save file with several retries without closing my project, then it > gets saved. > > Regards. Edit: However, i added a catch ex as exception handler and received the exception as "the file is being used by another process" although nothing is open except my program. |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
Edit: However, i added a catch ex as exception handler and received
the exception as "the file is being used by another process" although nothing is open except my program. Is it a streamwriter bug? Because nothing is using a text file while i'm trying to save through my project. |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
> Edit: However, i added a catch ex as exception handler and received > the exception as "the file is being used by another process" although > nothing is open except my program. > > Is it a streamwriter bug? Because nothing is using a text file while > i'm trying to save through my project. no, it's not a bug in the streamwriter - it's a bug in your code. and if your code for loading the file is similar to the saving code, then i can see why. you are never closing the resource. you need to call close on the streamwriter (and streamreader) when you are done with them.... using sr as new streamreader(myfile) ' do stuff end using using the using block will ensure that the streamreader is properly cleand up when the block ends. Basically, it will compile like you had done something like: dim sr as streamreader = nothing try sr = new streamreader(myfile) ' do stuff finally if not isnothing(sr) then sr.dispose() end finally you need to do the same with your streamreader. -- Tom Shelton |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote: > > > Edit: However, i added a catch ex as exception handler and received > > the exception as "the file is being used by another process" although > > nothing is open except my program. > > > Is it a streamwriter bug? Because nothing is using a text file while > > i'm trying to save through my project. > > no, it's not a bug in the streamwriter - it's a bug in your code. and > if your code for loading the file is similar to the saving code, then > i can see why. you are never closing the resource. you need to call > close on the streamwriter (and streamreader) when you are done with > them.... > > using sr as new streamreader(myfile) > ' do stuff > end using > > using the using block will ensure that the streamreader is properly > cleand up when the block ends. Basically, it will compile like you > had done something like: > > dim sr as streamreader = nothing > try > sr = new streamreader(myfile) > ' do stuff > finally > if not isnothing(sr) then > sr.dispose() > end finally > > you need to do the same with your streamreader. > > -- > Tom Shelton I was using this before i posted with no help: Try ...... Catch Finally If Not myStreamWriter Is Nothing Then myStreamWriter.Close() End If |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote: > > > Edit: However, i added a catch ex as exception handler and received > > the exception as "the file is being used by another process" although > > nothing is open except my program. > > > Is it a streamwriter bug? Because nothing is using a text file while > > i'm trying to save through my project. > > no, it's not a bug in the streamwriter - it's a bug in your code. and > if your code for loading the file is similar to the saving code, then > i can see why. you are never closing the resource. you need to call > close on the streamwriter (and streamreader) when you are done with > them.... > > using sr as new streamreader(myfile) > ' do stuff > end using > > using the using block will ensure that the streamreader is properly > cleand up when the block ends. Basically, it will compile like you > had done something like: > > dim sr as streamreader = nothing > try > sr = new streamreader(myfile) > ' do stuff > finally > if not isnothing(sr) then > sr.dispose() > end finally > > you need to do the same with your streamreader. > > -- > Tom Shelton I was using this for streamwriter also of streamreader before i posted with no help: Try ...... Catch Finally If Not myStreamWriter Is Nothing Then myStreamWriter.Close() End If |
|
|
|
#7 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
Hi,
Are you trying to overwirte a file that already exist? The stream reader may be trying to open a file that is use, or may be trying to append the data. When your calling for the write code, try this Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriter Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*" Save.CheckPathExists = True Save.Title = "Save" ' ----- Added Code ----------- If Save.ShowDialog(Me) = DialogResault.OK then Try ' ---------- Added Code ------------- If File.Exist(Save.Filename) then File.Delete (Save.FileName) myStreamWriter = System.IO.File.CreateText(Save.FileName) myStreamWriter.Write(TextBox1.Text) myStreamWriter.Flush() Catch ex As Exception ' Do nothing on Exception ' ---- I would put this code in to see what error is occuring, if any... Msgbox(ex.ToString) End Try End IF End Sub ------------ Hope this helps, let me know. -- Ryan S. Thiele RT Enterprises The Power is in your hands now! |
|
|
|
#8 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 8, 6:01 am, "Ryan S. Thiele"
<administra...@carriercompanion.com> wrote: > Hi, > > Are you trying to overwirte a file that already exist? The stream reader may > be trying to open a file that is use, or may be trying to append the data. > > When your calling for the write code, try this > > Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As _ > System.Object, ByVal e As System.EventArgs) Handles > SaveAsToolStripMenuItem.Click > Dim Save As New SaveFileDialog() > Dim myStreamWriter As System.IO.StreamWriter > > Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*" > Save.CheckPathExists = True > Save.Title = "Save" > ' ----- Added Code ----------- > If Save.ShowDialog(Me) = DialogResault.OK then > Try > ' ---------- Added Code ------------- > If File.Exist(Save.Filename) then File.Delete > (Save.FileName) > > myStreamWriter = System.IO.File.CreateText(Save.FileName) > myStreamWriter.Write(TextBox1.Text) > myStreamWriter.Flush() > Catch ex As Exception > ' Do nothing on Exception > ' ---- I would put this code in to see what error is > occuring, if any... > Msgbox(ex.ToString) > End Try > End IF > End Sub > > ------------ > Hope this helps, let me know. > > -- > Ryan S. Thiele > RT Enterprises > The Power is in your hands now! Hi Ryan, Yes, the problem occurs when i'm trying to overwrite an existing file. However i re-edited the code you suggested (just correct some typing errors) as this: ' ----- Added Code ----------- If Save.ShowDialog(Me) = DialogResult.OK Then Try ' ---------- Added Code ------------- If System.IO.File.Exists(Save.FileName) Then System.IO.File.Delete(Save.FileName) myStreamWriter = System.IO.File.CreateText(Save.FileName) myStreamWriter.Write(TextBox1.Text) myStreamWriter.Flush() Catch ex As Exception MsgBox(ex.Message) End Try End If But again and again, i receive that the file is being used by another process when i try to save but not always. |
|
|
|
#9 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
Could you use this code:
Dim Save As New SaveFileDialog() Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*" Save.CheckPathExists = True Save.Title = "Save" If Save.ShowDialog(Me) = DialogResult.OK Then Try Dim myStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(Save.FileName) myStreamWriter.Write(TextBox1.Text) myStreamWriter.Close() myStreamWriter.Dispose() Catch ex As Exception MsgBox(ex.ToString) End Try End If It will overwrite the existing file if it exists, otherwise it creates a new file. -Teemu |
|
|
|
#10 |
|
Guest
Posts: n/a
|
Re: StreamWriter Problem
On Dec 7, 5:01 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote:
> On Dec 8, 1:45 am, Tom Shelton <tom_shel...@comcast.net> wrote: > > > > > > > On Dec 7, 4:23 pm, kimiraikkonen <kimiraikkone...@gmail.com> wrote: > > > > Edit: However, i added a catch ex as exception handler and received > > > the exception as "the file is being used by another process" although > > > nothing is open except my program. > > > > Is it a streamwriter bug? Because nothing is using a text file while > > > i'm trying to save through my project. > > > no, it's not a bug in the streamwriter - it's a bug in your code. and > > if your code for loading the file is similar to the saving code, then > > i can see why. you are never closing the resource. you need to call > > close on the streamwriter (and streamreader) when you are done with > > them.... > > > using sr as new streamreader(myfile) > > ' do stuff > > end using > > > using the using block will ensure that the streamreader is properly > > cleand up when the block ends. Basically, it will compile like you > > had done something like: > > > dim sr as streamreader = nothing > > try > > sr = new streamreader(myfile) > > ' do stuff > > finally > > if not isnothing(sr) then > > sr.dispose() > > end finally > > > you need to do the same with your streamreader. > > > -- > > Tom Shelton > > I was using this for streamwriter also of streamreader before i posted > with no help: > Try > ..... > Catch > > Finally > If Not myStreamWriter Is Nothing Then > myStreamWriter.Close() > End If- Hide quoted text - > > - Show quoted text - There are only two possibilties here... 1) You aren't closing one of your streams properly - that's why I suggest using a using block, since it will guarentee they are closed. 2) The file really is locked by another process. I would suggest for one, that you make sure you aren't "swallowing" exceptions. Exceptions that are causing you to bypass your close. The close in every case should be in a finally block, or you should use the stream in a using block. This is not a bug in the stream classes - there is a bug in your code somewhere, or we do not have sufficient information to give you a proper answer. -- Tom Shelton |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |