StreamWriter Opening and Closing

Posted on 2006-04-03
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


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 I have to close one stream to allow the other to do some writing..then reopen it when the function returns.
Question by:SGyves
    LVL 85

    Accepted Solution

    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.
    LVL 14

    Assisted Solution

    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)
        End Sub

        Public Sub WriteLine(ByVal sText As String)
        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
        End Sub
    #End Region

    End Class

    Author Comment

    I will probably just go for simplicity for now and learn about the IDispose black magic later.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now