[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Using System.IO.StreamWriter

How can I determine the state of a stream writer object?  I would like to know if it is open so that I can close it in my try catch finally block.  
Howard Bash
Howard Bash
2 Solutions
Just add this

If IsNothing(strmwrtr) = False Then
End If

It would close itself and clear itself from memory.
Todd GerbertIT ConsultantCommented:
Use a "Using" block: http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx

Imports System.IO
Module Module1

    Sub Main()

            Using sw As New StreamWriter(New MemoryStream())
                For i As Integer = 0 To 10
                    sw.WriteLine("This is line " & i)
            End Using 'sw is guaranteed to be disposed here whether an exception occurs or not
        Catch ex As Exception
            Console.WriteLine("An error occurred.")
        End Try
    End Sub

End Module

Open in new window

Howard BashSenior Software EngineerAuthor Commented:
So, in a nutshell there is not status property or method and the way to "close" the stream is to dispose of it if it has been instantiated?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

I have not seen any status property. Disposing it would close it as well.
Todd GerbertIT ConsultantCommented:
Yes. In the case of a finally block, as you've described, there is no need to know the status.
Todd GerbertIT ConsultantCommented:
Doh! I gotta learn to type quicker. ;)
Carlos VillegasFull Stack .NET DeveloperCommented:
If your intention is to close it then tgerbert ways is the best choice.

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now