We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

StreamWriter Opening and Closing

SGyves
SGyves asked
on
Medium Priority
494 Views
Last Modified: 2012-08-13
I know how to create a StreamWriter like so:

Dim srFileWriter As New System.IO.StreamWriter("RecResults.txt", True)

close it like this

srFileWriter.Close()


but say I want to reopen that file for more writing after I close it. How do I do that with the same object? You see two functions trade off writing to this file...so I have to close one stream to allow the other to do some writing..then reopen it when the function returns.
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Don't close the StreamWriter.  Instead either pass the StreamWriter instance to your functions so you can use the same instance or declare it at a higher level (Form or Global) so it can be accessed from both places.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
You might consider writing a disposable class for it.

Imports System.IO

Public Class CFileWriter
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls
    Private _srFile As StreamWriter

    Public Sub New(ByVal sFileName As String, ByVal append As Boolean)
        _srFile = New StreamWriter(sFileName, append)
    End Sub

    Public Sub Write(ByVal sText As String)
        _srFile.Write(sText)
    End Sub

    Public Sub WriteLine(ByVal sText As String)
        _srFile.WriteLine(sText)
    End Sub

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If Not _srFile Is Nothing Then _srFile.Dispose()
            End If
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Author

Commented:
I will probably just go for simplicity for now and learn about the IDispose black magic later.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.