File.OpenHandle 方法

定義

SafeFileHandle使用指定的路徑、建立模式、讀取/寫入與共享許可權、其他 SafeFileHandles 可以對相同檔案、其他檔案選項和配置大小所擁有的存取權,初始化類別的新實例。

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

SafeFileHandle 執行個體。

例外狀況

pathnull

path 為空字串 (""),只包含空格或包含一或多個無效字元。 -或- path 指的是 NTFS 環境中的非檔案裝置,例如 CON:COM1:LPT1:等等。

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

preallocationSize 為負。 -或- modeaccessshare 包含不正確值。

找不到檔案,例如當 modeTruncateOpen,而且 path 指定的檔案不存在。 這些模式中必須有此檔案。

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

呼叫端沒有必要的權限。

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

access指定的作業系統不允許所要求的 path ,例如當 accessWriteReadWrite ,而且檔案或目錄設定為唯讀存取時。 -或- Encrypted 為指定 options ,但目前的平臺不支援檔案加密。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

適用於