Visual Basic Language Concepts 
How to: Write to Binary Files in Visual Basic 

The My.Computer.FileSystem.WriteAllBytes Method writes data to a binary file. If the append parameter is True, it will append the data to the file; otherwise data in the file is overwritten.

If the specified path excluding the file name is not valid, a DirectoryNotFoundException exception will be thrown. If the path is valid but the file does not exist, the file will be created.

To write to a binary file

  • Use the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData.dat.

    Visual Basic
    My.Computer.FileSystem.WriteAllBytes _
    ("C:\MyDocuments\CustomerData", CustomerData, True)

Robust Programming

The following conditions may create an exception:

  • The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; or it contains invalid characters. (ArgumentException).

  • The path is not valid because it is Nothing (ArgumentNullException).

  • File points to a path that does not exist (FileNotFoundException or DirectoryNotFoundException).

  • The file is in use by another process, or an I/O error occurs (IOException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).

  • The user lacks necessary permissions to view the path (SecurityException).

See Also

Tags :


Community Content

David Streeter
More useful sample

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'specify the filename
        Dim strFilename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Test.tmp"
        'create a filestream object, this is something like a file handle in VB6
        Dim fsOutput As New FileStream(strfilename, FileMode.Create, FileAccess.Write, FileShare.None)
        'create a binary writer object, that knows how to encode variables into a file
        Dim bwOutput As New BinaryWriter(fsOutput)
        'set up some variables to write
        Dim intOne As Integer = 12
        Dim strTwo As String = "abcdefg"
        'write the integer
        bwOutput.Write(intOne)
        'write the string
        bwOutput.Write(strTwo)
        'close and dispose of file writing objects
        bwOutput.Close()
        fsOutput.Close()
        fsOutput.Dispose()
        'create a filestream object, this is something like a file handle in VB6
        Dim fsInput As New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.None)
        'create a binary reader object, that knows how to decode variables from a file
        Dim brInput As New BinaryReader(fsInput)
        'set up target variables
        Dim intThree As Integer
        Dim strFour As String
        'read the integer
        intThree = brInput.ReadInt32()
        'read the string
        strFour = brInput.ReadString
        'display the variables so we know they loaded correctly
        MsgBox("Integer = " & intThree.ToString & vbCr & "String = " & strFour)
        'close and dispose the file reading objects
        brInput.Close()
        fsInput.Close()
        fsInput.Dispose()
        'delete the file
        Kill(strFilename)
    End Sub
End Class
 
Tags :

Page view tracker