FileSystem.ReadAllBytes(String) 方法

定义

以字节数组的形式返回文件的内容。Returns the contents of a file as a byte array.

public:
 static cli::array <System::Byte> ^ ReadAllBytes(System::String ^ file);
public static byte[] ReadAllBytes (string file);
static member ReadAllBytes : string -> byte[]
Public Shared Function ReadAllBytes (file As String) As Byte()

参数

file
String

要读取的文件。File to be read.

返回

Byte[]

包含文件内容的 Byte 数组。Byte array containing the contents of the file.

例外

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头);以尾部反斜杠结尾。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.

fileNothingfile is Nothing.

文件不存在。The file does not exist.

文件正由另一个进程使用,或者出现 I/O 错误。The file is in use by another process, or an I/O error occurs.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

内存不足,无法将字符串写入缓冲区。There is not enough memory to write the string to buffer.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

示例

此示例从文件读取 C:/Documents and Settings/selfportrait.jpg.This example reads from the file C:/Documents and Settings/selfportrait.jpg.

My.Computer.FileSystem.ReadAllBytes(
  "C:/Documents and Settings/selfportrait.jpg")

注解

使用 ReadAllBytes 对象的方法可以 My.Computer.FileSystem 从二进制文件中读取。The ReadAllBytes method of the My.Computer.FileSystem object allows you to read from a binary file. 文件的内容作为字节数组返回。The contents of the file are returned as a byte array.

不要根据文件的名称来判断文件的内容。Do not make decisions about the contents of the file based on the name of the file. 例如,文件 Form1.vb 可能不是 Visual Basic 源文件。For example, the file Form1.vb may not be a Visual Basic source file. 在应用程序中使用输入的数据之前,需验证所有的输入内容。Verify all inputs before using the data in your application.

下表列出了涉及方法的任务的示例 My.Computer.FileSystem.ReadAllBytesThe following table lists an example of a task involving the My.Computer.FileSystem.ReadAllBytes method.

功能To 查看See
从二进制文件读取Read from a binary file 如何:在 Visual Basic 中读取二进制文件How to: Read From Binary Files in Visual Basic

适用于