0

trying to convert a VB6 program just now, it uses FILESYSTEMOBJECT to create a TEXTSTREAM for appending, and once opened it is kept open for the whole process as this is the method of writing to the LOG files. The recommended method seems to be SYSTEM.IO for a replacement.

All the info I can find (FILES and STREAMWRITER) all seem to Open / Write / Close, the file every time we want to append to it. We are looking to keep the file open and the stream active to remove any overheads in the OPEN / CLOSE for each write. New to VB.NET so any pointers are greatly appreciated.

One constraint we have is that we can't build up multiple lines of text and then write them all at once, the business need it written to after each action

I have experimented with USING - END USING for TEXTSTREAMS and FILE.CREATE / APPENDALLText

asked Feb 22, 2023 at 22:51

2 Answers 2

1

You can definitely use a StreamWriter. Just don't close it until you're ready to.

If you need to access it from multiple methods, then declare it class/form level and it'll stay open for the lifetime of the app, or until you explicitly close it:

Imports System.IO
Public Class Form1
 Private sw As StreamWriter
 Private fullPathFileName = "c:\some folder\path\fileName.txt"
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 sw = New StreamWriter(fullPathFileName, True)
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 ' ... do something in here, probably in a loop ...
 While (someConditions)
 ' Write to "sw":
 sw.WriteLine("New Data Line Appended!")
 End While
 End Sub
 Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
 If Not IsNothing(sw) Then
 sw.Close()
 End If
 End Sub
End Class

*The above has zero exception handling, which should always be added when dealing with files.

answered Feb 23, 2023 at 0:18
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers for the advice, it was still causing issues if something went wrong i.e. crashed. The stream is only written to once you close the stream. Your advice helped me see it in action and look at some of the other methods etc. Setting sw.AutoFlush in the form load allowed for the stream to write immediately that it is called. Thanks for your time, saved me lots of headaches
-1
Using writer As New StreamWriter("c:\temp\myLog.txt", True)
 writer.WriteLine("my Log Message")
End Using
answered Feb 22, 2023 at 23:37

3 Comments

Hi, thanks for taking the time to respond. Unfortunately the Using - End Using Opens and Closes the file for each Write we wish to do. We need to keep the file open between writes.
You can place the rest of your code inside the using or take care of explicitly disposing the writer object. The logic is the same.
Cheers again for getting back, it may work for us in some parts if not I will keep the idea in mind, cheers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.