if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWr

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

Daniel

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.
 
You can use the using statment do something like this

using(StreamWriter sw=new StreamWriter(fs))
{
// do something you like.
}

In this case, even this an exception, it will be closed.
 
another option besides using

StreamWriter sw;
try{
.. use sw here
}
catch(Exception){
handle excpetion here
}
finally{
sw.close();
}
 
This will work, but personally, I think the using statement is much
cleaner solution to this kind of issue.
 

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