Reputation: 478
I got a file stream:
tempOutputStream = new FileStream(sTempFileName, FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.ReadWrite);
Once the file is created you can create additional FileStreams to read/write tempOutputStream
.
Is there a way to close all of them not having a ref to each stream obj? Or at least for the first one that creates the stream.
the problem is that I'd like to delete the file but one of the streams is not closed so I got an exception in File.Delete
...
Thank you.
Regards, Leonid
Upvotes: 1
Views: 4647
Reputation: 19976
One possible solution:
GetNewStream()
List<Stream>
Close()
, and Close()
each stream from the listDispose()
, and Dispose()
each stream from the listThat way you'll hide your functionality where it should be, away from you in the main line of code.
Another, to prevent hard refactoring:
Stream s=originalStream.GetStream();
in your code (I don't know by heart how the stream is duplicated...)Map<Stream, list<Stream>>
and several methods:
RegisterStreamChild(Stream Parent, Stream Child);
CloseAllChildSteams(Stream Parent)
Upvotes: 1
Reputation: 887867
No, there isn't.
You need to fix your code and ensure that all streams are closed when you finish using them.
Use the using
statement.
Upvotes: 1