Skip to content

Latest commit

 

History

History
44 lines (28 loc) · 2.17 KB

how-to-write-to-binary-files.md

File metadata and controls

44 lines (28 loc) · 2.17 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: How to: Write to Binary Files in Visual Basic
How to: Write to Binary Files
07/20/2015
files [Visual Basic], binary access
WriteAllBytes method [Visual Basic]
binary files [Visual Basic], writing in Visual Basic
59fae125-de5b-4c96-883c-209f4a55112c

How to: Write to Binary Files in Visual Basic

The xref:Microsoft.VisualBasic.FileIO.FileSystem.WriteAllBytes%2A 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 xref:System.IO.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.

[!code-vbVbVbcnMyFileSystem#27]

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. (xref:System.ArgumentException).

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

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

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

  • The path exceeds the system-defined maximum length (xref:System.IO.PathTooLongException).

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

  • The user lacks necessary permissions to view the path (xref:System.Security.SecurityException).

See also