File.OpenHandle 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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 。
- access
- FileAccess
列舉值的位元組合,可決定如何存取檔案。 預設值為 Read
- options
- FileOptions
物件,描述 SafeFileHandle 要使用的選擇性參數。
- preallocationSize
- Int64
檔案的初始配置大小(以位元組為單位)。 只有在建立、覆寫或取代一般檔案時,正值才會生效。 不允許負數值。 在其他情況下 (包括預設值0值) ,則會予以忽略。
傳回
SafeFileHandle 執行個體。
例外狀況
path
為 null
。
path
為空字串 (""),只包含空格或包含一或多個無效字元。
-或- path
指的是 NTFS 環境中的非檔案裝置,例如 CON:
、 COM1:
、 LPT1:
等等。
path
指非 NTFS 環境中的非檔案裝置,例如 CON:
、 COM1:
、 LPT1:
等等。
preallocationSize
為負。
-或- mode
、 access
或 share
包含不正確值。
發生 I/O 錯誤,例如當 path
指定的檔案已存在時指定 CreateNew。
-或-當提供時,磁片已滿 (preallocationSize
,且 path
指向一般檔案) 。
-或-當提供時,檔案太大 (preallocationSize
,且 path
指向一般檔案) 。
呼叫端沒有必要的權限。
指定的路徑無效,例如位於未對應的磁碟機上。
access
指定的作業系統不允許所要求的 path
,例如當 access
是 Write 或 ReadWrite ,而且檔案或目錄設定為唯讀存取時。
-或- Encrypted 為指定 options
,但目前的平臺不支援檔案加密。
指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。