ADO DOT NET
Centurion
- Joined
- Dec 20, 2006
- Messages
- 160
Visual Basic:
Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click
Try
Dim FilePath As FileStream = Nothing
For Counter As Integer = 1 To Some Number
FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)
...process
FilePath.Close()
Next
Catch x As x
...
Finally
FilePath.Close()
End Try
End Sub
But it is possible that some time due to an error control goes outside of the loop into the catch section without closing the file stream.
So I put a FilePath.Close() in the finally section to make sure that file is closed.
But if no error occurs, I get another error for FilePath.Close() in the finally section! I cannot close an already closed stream.
What should I do for such case? What do you recommend?
Thanks all