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 是一个空字符串 (""),仅包含空格,或者包含一个或多个无效字符。

  • 或 - 指 NTFS 环境中的非文件设备,例如 path CON:、COM1:、LPT1:等。

path引用非 NTFS 环境中的非文件设备,例如CON:、COM1:、LPT1:等。

preallocationSize 为负数。

  • mode - 、 或 access share 包含无效值。

找不到该文件。例如,当 modeTruncateOpen,并且 path 指定的文件不存在时会出现这种情况。 该文件必须以这些模式存在。

I/O 错误,例如在 path 指定的文件已存在时指定 CreateNew,会发生此错误。

  • 或 - 提供 时 (preallocationSize 磁盘已满, path 并且指向常规文件) 。
  • 或 - 提供 时, (太大, preallocationSize path 并且指向常规文件) 。

调用方没有所要求的权限。

指定的路径无效,例如位于未映射的驱动器上。

指定的 的操作系统不允许请求的 ,例如 当 为 或 并且文件或目录设置为只读 access path access Write ReadWrite 访问时。

  • 或 - Encryptedoptions 指定,但当前平台上不支持文件加密。

指定的路径和/或文件名超过了系统定义的最大长度。

适用于