By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

StreamReader refuses to let go of file

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google.c om...
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

Nov 20 '05 #2

P: n/a
Have you tried the Dispose method?

Mike.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:uS*************@TK2MSFTNGP10.phx.gbl...
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google.c om...
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


Nov 20 '05 #3

P: n/a
Have you tried the Dispose method?

Mike.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:uS*************@TK2MSFTNGP10.phx.gbl...
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google.c om...
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


Nov 20 '05 #4

P: n/a

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
-----Original Message-----
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google. com...
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

.

Nov 20 '05 #5

P: n/a

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
-----Original Message-----
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google. com...
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

.

Nov 20 '05 #6

P: n/a
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
"jamie" <an*******@discussions.microsoft.com> wrote in message news:<7b****************************@phx.gbl>...
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
-----Original Message-----
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google. com...
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

.

Nov 20 '05 #7

P: n/a
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
"jamie" <an*******@discussions.microsoft.com> wrote in message news:<7b****************************@phx.gbl>...
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
-----Original Message-----
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

"Daniel" <dc@yachana.se> wrote in message
news:58**************************@posting.google. com...
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

.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.