Clearing File Streams on Error
Hello,
I have an application which opens various files for reading and writing. The problem I'm having is, if the application errors for whatever reason, the file streams stay open, so next time I run the application I get a "Cannot open file as it is being used by another process.".
The only way to get around this is by ending the task aspnet_wp.exe in task manager. I can't do this in a live environment.
Does anyone know of a solution to this. I was thinking of adding some code in the Application_Error event to check which filestreams are open, and ending them.
Cheers!
Re: Clearing File Streams on Error
Put your streams in a try catch block.
VB Code:
Try
'some code
Catch ex as Exception
Throw
Finally
stream.close
End Try
Finally blocks will run no matter what, so if your code errors inside the Try block, the Catch block will throw the error and the Finally block will execute