語言

File.OpenHandle 方法

定義

初始化類別的新實例 SafeFileHandle ,包含指定的路徑、建立模式、讀寫與共享權限、其他安全檔案句柄對同一檔案的存取權限、額外檔案選項及配置大小。

public static Microsoft.Win32.SafeHandles.SafeFileHandle OpenHandle(string path, System.IO.FileMode mode = System.IO.FileMode.Open, System.IO.FileAccess access = System.IO.FileAccess.Read, System.IO.FileShare share = System.IO.FileShare.Read, System.IO.FileOptions options = System.IO.FileOptions.None, long preallocationSize = 0);
static member OpenHandle : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare * System.IO.FileOptions * int64 -> Microsoft.Win32.SafeHandles.SafeFileHandle
Public Shared Function OpenHandle (path As String, Optional mode As FileMode = System.IO.FileMode.Open, Optional access As FileAccess = System.IO.FileAccess.Read, Optional share As FileShare = System.IO.FileShare.Read, Optional options As FileOptions = System.IO.FileOptions.None, Optional preallocationSize As Long = 0) As SafeFileHandle

參數

path
String

一個檔案的相對或絕對路徑,目前實例會封裝它 SafeFileHandle 。

mode
FileMode

其中一個列舉值,決定如何開啟或建立檔案。 預設值是 Open

access
FileAccess

一個位元組合的列舉值,決定檔案的存取方式。 預設值是 Read

share
FileShare

列舉值的位元組合,決定進程將如何共享檔案。 預設值為 Read。

options
FileOptions

一個描述可選 SafeFileHandle 參數的物件。

preallocationSize
Int64

檔案的初始配置大小,以位元組為單位。 正值僅在建立、覆寫或替換一般檔案時才有效。 不允許使用負數值。 在其他情況下(包括預設的 0 值),則會被忽略。

傳回

SafeFileHandle實例。

例外狀況

path 是 null。

path 是空字串(“”),僅包含空白,或包含一個或多個無效字元。 -or- path 指的是非檔案裝置,例如 CON:、 COM1:、 LPT1:、 等,在 NTFS 環境中。

path指的是非檔案裝置,例如在非NTFS環境中的CON:、COM1:LPT1:等。

preallocationSize 為負數。 -或- mode, access, , 或 share 包含無效值。

找不到該檔案,例如當 mode 是 或 TruncateOpen時,且指定的 path 檔案不存在。 檔案必須已存在於這些模式中。

發生了 I/O 錯誤,例如指定 CreateNew 何時指定的檔案 path 已經存在。 -或- 磁碟已滿(提供時preallocationSizepath指向一般檔案)。 -或者- 檔案太大(當 preallocationSize 提供 path 時指向一般檔案)。

來電者沒有所需的權限。

指定的路徑無效,例如位於未對應的磁碟驅動器上。

access作業系統不允許對指定的 path,例如當 access 是 或 WriteReadWrite 且檔案或目錄設定為唯讀存取時。 -or- Encrypted 在 中指定, options但目前平台不支援檔案加密。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

適用於