Share via


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 執行個體。

例外狀況

pathnull

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

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

preallocationSize 為負。 -或- modeaccessshare 包含無效的值。

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

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

呼叫端沒有必要的權限。

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

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

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

適用於