语言

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 。

例外

path 是 null。

path 为空字符串(“”),仅包含空格,或包含一个或多个无效字符。 - 或 - path 指 NTFS 环境中的非文件设备,例如CON:COM1:LPT1:,等等。

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

preallocationSize 为负数。 - 或 - mode, access或 share 包含无效值。

找不到该文件,例如何时modeTruncate或Open不存在指定的path文件。 该文件必须已存在于这些模式中。

出现 I/O 错误,例如指定 CreateNew 文件 path 已存在的时间。 - 或 - 磁盘已满(何时 preallocationSize 提供并 path 指向常规文件)。 - 或 - 文件太大(何时 preallocationSize 提供并 path 指向常规文件)。

调用方没有所需的权限。

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

access指定的操作系统path不允许请求,例如何时accessWrite或ReadWrite设置文件或目录进行只读访问。 -or- Encrypted 已指定 options,但当前平台上不支持文件加密。

指定的路径、文件名或两者都超过了系统定义的最大长度。

适用于