Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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.

    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

Community Additions

ADD
Show:
© 2015 Microsoft