FileSystem.ReadAllBytes(String) 方法

定義

以位元組陣列的形式回傳檔案內容。

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

待閱讀的檔案。

傳回

Byte[]

Byte 陣列包含檔案內容。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

fileNothing

檔案不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

記憶體不足以將字串寫入緩衝區。

使用者缺乏必要的權限來查看路徑。

範例

此範例從檔案中讀取 C:/Documents and Settings/selfportrait.jpg.

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

備註

ReadAllBytes物件的方法My.Computer.FileSystem允許你從二進位檔案讀取。 檔案內容會以位元組陣列的形式回傳。

請勿根據檔案名稱來判斷檔案內容。 例如,檔案 Form1.vb 可能不是 Visual Basic 來源檔案。 在應用程式中使用這些資料之前,請先驗證所有輸入值。

下表列出一個涉及此 My.Computer.FileSystem.ReadAllBytes 方法的任務範例。

看!
從二進位檔讀取 如何:從 Visual Basic

適用於