StreamWriter Problem

K

kimiraikkonen

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.
 
K

kimiraikkonen

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.
 
K

kimiraikkonen

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.
 
T

Tom Shelton

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.
 
K

kimiraikkonen

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.

I was using this before i posted with no help:
Try
......
Catch

Finally
If Not myStreamWriter Is Nothing Then
myStreamWriter.Close()
End If
 
K

kimiraikkonen

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.

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
 
R

Ryan S. Thiele

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
 
K

kimiraikkonen

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

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.
 
T

Teemu

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
 
T

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.
 
K

kimiraikkonen

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.

Teemu and Tom,
I'm currently using this code with getting this exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()

However, i added a yesnocancel msgbox question inside exception area
against the problem, when this exception occurs user can retry re-save
or cancel save process. At least this saves it temporarily but i wish
i didn't have to yesnocancel question prompt to retry save process.
 
K

kimiraikkonen

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.


Teemu and Tom,
I'm currently using this code for "try" area with getting this
exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()


However, i added a "yesnocancel" msgbox question inside exception area
against the problem, when this exception occurs user can retry save
process
or cancel save process. At least this does it temporarily but i wish
i didn't have to yesnocancel question prompt to retry save process.
 
T

Teemu

Teemu and Tom,
I'm currently using this code for "try" area with getting this
exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()


However, i added a "yesnocancel" msgbox question inside exception area
against the problem, when this exception occurs user can retry save
process
or cancel save process. At least this does it temporarily but i wish
i didn't have to yesnocancel question prompt to retry save process.

Your problem sounds so weird that I can't figure out any reason for this if
the file is really not used by another program.

Maybe you could use a tool like this
http://software.techrepublic.com.com/download.aspx?&kw=File+lock&docid=211273
to check out which process has locked the file when the exception occurs.

-Teemu
 
K

kimiraikkonen

"kimiraikkonen" <[email protected]> kirjoitti viestissä




Your problem sounds so weird that I can't figure out any reason for this if
the file is really not used by another program.

Maybe you could use a tool like thishttp://software.techrepublic.com.com/download.aspx?&kw=File+lock&doci...
to check out which process has locked the file when the exception occurs.

-Teemu

Hi,
Thanks for the tool but it did nothing, however this problem happens
very rarely for the latest code that i've edited, but and happened
again. With exception window is stayed open, i used the tool you
recommended, but it didn't report any process that locks my text file,
only says: "You do not have sufficient access to 1 processes". I have
all the rights as i'm admin, and i saw some right-related problem
about that exception, but i couldn't find an exact solution.
 
K

kimiraikkonen

"kimiraikkonen" <[email protected]> kirjoitti viestissä




Your problem sounds so weird that I can't figure out any reason for this if
the file is really not used by another program.

Maybe you could use a tool like thishttp://software.techrepublic.com.com/download.aspx?&kw=File+lock&doci...
to check out which process has locked the file when the exception occurs.

-Teemu

Hi,
Thanks for the tool but it did nothing, however this problem happens
very rarely for the latest code that i've edited, but and happened
again. With exception window is stayed open, i used the tool you
recommended, but it didn't report any process that locks my text file,
only says: "You do not have sufficient access to 1 processes". I have
all the rights as i'm admin, and i saw some right-related problem
about that exception, but i couldn't find an exact solution.

Additionaly, to test the tool which you pointed out, while an mp3
audio file is being played by a player, i used that tool to report if
it reports correct process(player) as it's used by, but nothing is
reported. This tool is not reliable.
 
T

Teemu

Additionaly, to test the tool which you pointed out, while an mp3
audio file is being played by a player, i used that tool to report if
it reports correct process(player) as it's used by, but nothing is
reported. This tool is not reliable.

OK. I didn't try that tool myself so I don't know if it works or not. Maybe
you find another tool similar to this which works better. That kind of tool
might be the only way to find out what happens.

Is there any difference between running with debugger or only the
executable? Could it be possible that debugger won't release the file
sometimes and keeps it locked? Or does it matter how often you try to save
the file?

-Teemu
 
T

tom_shelton

kimiraikkonen said:
Teemu and Tom,
I'm currently using this code for "try" area with getting this
exception rarely but
not solved completely:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Flush()
myStreamWriter.Close()
myStreamWriter.Dispose()

This is all you need - your working to hard.
Try

Using myStreamWriter As StreamWriter = File.CreateText (Save.FileName)
myStreamWriter.Write (TextBox1.Text)
myStreamWriter.Flush()
End Using

Catch ex As Exception
MessageBox.Show (ex.Message)
End Try


Put all of your reader's writers in a using block. It will call dispose
(which will close the file), even if you get an exception in the block.

There is no threading going on in this app?
 
K

kimiraikkonen

Do you realize all of your posts to this thread are posted twice?

If you're asking my posts, none of them was sent twice, but if you see
total message number higher than actual messages before clicking on
thread, that's because i needed to remove and re-post the message due
to need of modiying, not message duplication.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top