StreamReader refuses to let go of file

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

I have an winform application that uses System.IO.StreamReader and
Serializer.Deserialize to load some data from a file, if any error is
detected in the file a new file should be written to the same
directory, I use a StreamWriter and Serializer.Serialize to do this.
The problem is that when the streamreader has opened the file it does
not release it until the application is closed.

When I try to use the StreamWriter to write the new file I thus get
"System.IO.IOException: The process cannot .... because it is being
used by another process".

I have tried with closing the streamreader (obviously) and setting all
possible objects to nothing as well as call the gc explicitly. Using
Process explorer from sysinternals I can close the handle to the file
if I use the IDE and take it step by step and then it works.

Any and all ideas welcome
/Daniel
 
I cant remember of the top of my head, but is there a flush method ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Have you tried the Dispose method?

Mike.

One Handed Man ( OHM - Terry Burns ) said:
I cant remember of the top of my head, but is there a flush method ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
I would bet on the .flush then the .close, then if you are
so inclined: filestream1 = nothing

the flush forces any buffered data to the device
the close relieces any resorces associated.
the "filestream1 = nothing" is for fun.

good luck

jamie
 
Thanks for the tip, the streamreader does not seem to have a flush or
discard method (I think that close is equivalent do dispose) but it do
have a DiscardBufferedData method, but that did not help either ...

Created a small test project and added only the relevant code and for
some reason that works just fine with the file beeing released when
it's supposed to.
Checked in Filemon and the sequences for the file are identical for my
test project and the one that is giving me grief, both get CLOSE
requests that succeed but in one case the file is still locked.
Beginning to think that it's the serialization object that somehow
retains a link to the file even if I can't see how.

/Daniel
 

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

Back
Top