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

if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces

P: n/a
if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Daniel wrote:
if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(null);
// 1. an exception is thrown
}
catch {
Debug.WriteLine("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt
Nov 12 '05 #2

P: n/a
If my System.IO.StreamWriter Write method throws "The specified network name
is no longer available." and I try to Dispose or Close it in the finaly
clause the close or dispose method just throws "The specified network name
is no longer available." again. how to clean this up? after this the stream
writer is stuck connected until i restart my process.

"Pascal Schmitt" <ne*******@cebra.nu> wrote in message
news:en****************@TK2MSFTNGP12.phx.gbl...
Daniel wrote:
if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(null);
// 1. an exception is thrown
}
catch {
Debug.WriteLine("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Browse more .NET Framework Questions on Bytes