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 库函数,如预期的文件描述符, freadIt 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,并且它需要不同的参数ReadFileWriteFileIn 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. FileStream 可能会引发IOException如果FileStream检测到某些其他进程已移动的文件指针。FileStream might throw an IOException if FileStream detects that some other process has moved the file pointer. 若要避免此问题,不要不写入任何数据到文件的一部分的FileStream可能已缓冲,并还原到在上一次调用方法时的位置的文件指针FileStreamTo 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.

适用于

另请参阅