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

x
  • 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.  
0
Howard Bash
Asked:
Howard Bash
2 Solutions
 
CodeCruiserCommented:
Just add this

If IsNothing(strmwrtr) = False Then
   strmwrte.Dispose
End If


It would close itself and clear itself from memory.
0
 
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()
        Try

            Using sw As New StreamWriter(New MemoryStream())
                For i As Integer = 0 To 10
                    sw.WriteLine("This is line " & i)
                Next
            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

0
 
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?
0
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.

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

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