Properly closing OFileStream File

ejleiss

Active member
Joined
Oct 1, 2012
Messages
37
Programming Experience
1-3
Hi,

I am saving data from a serial port to a file using OFileStream. I am having trouble closing a file and I think it's because data is still being sent to the file when I'm trying to close it. All I have found online is the following code to close the file. Could anyone let me know if this is the right direction?

If Not oFileStream Is Nothing Then
oFileStream.Close()
End If
 
To close a FileStream you call its Close method. If you are creating and closing the FileStream within the same block then it would be preferable to use a Using statement, e.g.
VB.NET:
Using fs As New FileStream(...)
    'Use fs here
End Using
That same pattern can be used with any type that implements the IDisposable interface. The object is created with the Using statement and destroyed with the End Using statement.
 
Back
Top