FileStream.Handle FileStream.Handle FileStream.Handle FileStream.Handle Property

定义

警告

此 API 现已过时。

获取当前 FileStream 对象所封装文件的操作系统文件句柄。Gets the operating system file handle for the file that the current FileStream object encapsulates.

public:
 virtual property IntPtr Handle { IntPtr get(); };
[System.Obsolete("Use SafeFileHandle instead")]
[System.Obsolete("This property has been deprecated.  Please use FileStream's SafeFileHandle property instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
[get: System.Security.SecurityCritical]
[System.Obsolete("This property has been deprecated.  Please use FileStream's SafeFileHandle property instead.  https://go.microsoft.com/fwlink/?linkid=14202")]
public virtual IntPtr Handle { get; }
member this.Handle : nativeint
Public Overridable ReadOnly Property Handle As IntPtr

属性值

FileStream 对象所封装文件的操作系统文件句柄;如果 FileStream 已关闭,则为 -1。The operating system file handle for the file encapsulated by this FileStream object, or -1 if the FileStream has been closed.

异常

调用方没有所要求的权限。The caller does not have the required permission.

注解

此属性是与操作系统提供的系统调用(如ReadFile在 Windows 上)一起使用的操作系统句柄。This property is an operating system handle for use with operating-system-provided system calls (such as ReadFile on Windows). 它不会与需要文件描述符的 C 库函数(如fread)一起使用。It will not work with C library functions that expect a file descriptor, such as fread.

操作系统句柄可能已同步或异步打开,具体取决于调用的FileStream构造函数。The operating system handle might have been opened synchronously or asynchronously, depending on which FileStream constructor was called. IsAsync使用属性来发现此句柄是否已异步打开。Use the IsAsync property to discover whether this handle was opened asynchronously. 在 Win32 中,这意味着打开了重叠 IO 的句柄,它需要ReadFileWriteFile的不同参数。In Win32, this means the handle was opened for overlapped IO, and it requires different parameters to ReadFile and WriteFile.

注意

如果创建了一个FileStream ,则会发生数据损坏,而某些操作则会移动句柄的文件指针, FileStream然后再次使用。Data corruption might occur if a FileStream is created, its handle is passed, some operation moves the handle's file pointer, and then the FileStream is used again. 多个线程无法同时安全地写入同一文件,并且FileStream缓冲代码假定它以独占方式控制句柄。Multiple threads cannot safely write to the same file simultaneously, and FileStream buffering code assumes that it exclusively controls the handle. FileStreamIOException如果检测到其他进程已移动文件指针,则FileStream可能会引发。FileStream might throw an IOException if FileStream detects that some other process has moved the file pointer. 若要避免出现这种情况,请不要将任何数据写入FileStream可能已进行缓冲处理的文件的一部分,然后将文件指针还原到上一次FileStream调用方法时的位置。To avoid this, do not write any data into a portion of the file that FileStream might have buffered, and restore the file pointer to the location it had when methods were last called on FileStream.

安全性

SecurityPermission
用于访问非托管代码的能力。for the ability to access unmanaged code. 关联的枚举:UnmanagedCode安全操作: InheritanceDemandAssociated enumeration: UnmanagedCode Security action: InheritanceDemand.

SecurityCriticalAttribute
需要对直接调用方的完全信任。requires full trust for the immediate caller. 部分受信任的或透明的代码不能使用此成员。This member cannot be used by partially trusted or transparent code.

适用于

另请参阅