FileStream 类

定义

为文件提供 Stream,既支持同步读写操作,也支持异步读写操作。Provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.

public ref class FileStream : System::IO::Stream
[System.Runtime.InteropServices.ComVisible(true)]
public class FileStream : System.IO.Stream
type FileStream = class
    inherit Stream
Public Class FileStream
Inherits Stream
继承
属性

示例

下面的示例演示了某些 FileStream 构造函数。The following example demonstrates some of the FileStream constructors.

using namespace System;
using namespace System::IO;
using namespace System::Text;

void AddText( FileStream^ fs, String^ value )
{
   array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
   fs->Write( info, 0, info->Length );
}

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Delete the file if it exists.
   if ( File::Exists( path ) )
   {
      File::Delete( path );
   }

   //Create the file.
   {
      FileStream^ fs = File::Create( path );
      try
      {
         AddText( fs, "This is some text" );
         AddText( fs, "This is some more text," );
         AddText( fs, "\r\nand this is on a new line" );
         AddText( fs, "\r\n\r\nThe following is a subset of characters:\r\n" );
         for ( int i = 1; i < 120; i++ )
         {
            AddText( fs, Convert::ToChar( i ).ToString() );
            
            //Split the output at every 10th character.
            if ( Math::IEEERemainder( Convert::ToDouble( i ), 10 ) == 0 )
            {
               AddText( fs, "\r\n" );
            }
         }
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
   
   //Open the stream and read it back.
   {
      FileStream^ fs = File::OpenRead( path );
      try
      {
         array<Byte>^b = gcnew array<Byte>(1024);
         UTF8Encoding^ temp = gcnew UTF8Encoding( true );
         while ( fs->Read( b, 0, b->Length ) > 0 )
         {
            Console::WriteLine( temp->GetString( b ) );
         }
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
}
using System;
using System.IO;
using System.Text;

class Test
{

    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        //Create the file.
        using (FileStream fs = File.Create(path))
        {
            AddText(fs, "This is some text");
            AddText(fs, "This is some more text,");
            AddText(fs, "\r\nand this is on a new line");
            AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");

            for (int i=1;i < 120;i++)
            {
                AddText(fs, Convert.ToChar(i).ToString());

            }
        }

        //Open the stream and read it back.
        using (FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }

    private static void AddText(FileStream fs, string value)
    {
        byte[] info = new UTF8Encoding(true).GetBytes(value);
        fs.Write(info, 0, info.Length);
    }
}
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Delete the file if it exists.
        If File.Exists(path) Then
            File.Delete(path)
        End If

        'Create the file.
        Dim fs As FileStream = File.Create(path)

        AddText(fs, "This is some text")
        AddText(fs, "This is some more text,")
        AddText(fs, Environment.NewLine & "and this is on a new line")
        AddText(fs, Environment.NewLine & Environment.NewLine)
        AddText(fs, "The following is a subset of characters:" & Environment.NewLine)

        Dim i As Integer

        For i = 1 To 120
            AddText(fs, Convert.ToChar(i).ToString())

        Next

        fs.Close()

        'Open the stream and read it back.
        fs = File.OpenRead(path)
        Dim b(1023) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop

        fs.Close()
    End Sub

    Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
        fs.Write(info, 0, info.Length)
    End Sub
End Class

下面的示例演示如何以异步方式写入文件。The following example shows how to write to a file asynchronously. 此代码在具有名为 U s 的 TextBlock 的 WPF 应用中运行,并将按钮挂钩到名为 Button_Click 的 Click 事件处理程序。This code runs in a WPF app that has a TextBlock named UserInput and a button hooked up to a Click event handler that is named Button_Click. 文件路径需要更改为计算机上存在的文件。The file path needs to be changed to a file that exists on the computer.

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            UnicodeEncoding uniencoding = new UnicodeEncoding();
            string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";
           
            byte[] result = uniencoding.GetBytes(UserInput.Text);
            
            using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
            {
                SourceStream.Seek(0, SeekOrigin.End);
                await SourceStream.WriteAsync(result, 0, result.Length);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
        Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt"

        Dim result As Byte() = uniencoding.GetBytes(UserInput.Text)

        Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
            SourceStream.Seek(0, SeekOrigin.End)
            Await SourceStream.WriteAsync(result, 0, result.Length)
        End Using
    End Sub
End Class

注解

使用 FileStream 类可对文件系统中的文件进行读取、写入、打开和关闭,还可以操作其他与文件相关的操作系统句柄,包括管道、标准输入和标准输出。Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output. 您可以使用 ReadWriteCopyToFlush 方法来执行同步操作,或使用 ReadAsyncWriteAsyncCopyToAsyncFlushAsync 方法执行异步操作。You can use the Read, Write, CopyTo, and Flush methods to perform synchronous operations, or the ReadAsync, WriteAsync, CopyToAsync, and FlushAsync methods to perform asynchronous operations. 使用异步方法来执行占用大量资源的文件操作,而不会阻止主线程。Use the asynchronous methods to perform resource-intensive file operations without blocking the main thread. Windows 8.x 应用商店Windows 8.x Store 应用或 桌面desktop 应用中一个耗时的流操作可能阻塞 UI 线程并让您的应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。This performance consideration is particularly important in a Windows 8.x 应用商店Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. FileStream 缓冲输入和输出以提高性能。FileStream buffers input and output for better performance.

重要

此类型实现 IDisposable 接口。This type implements the IDisposable interface. 在使用完类型后,您应直接或间接释放类型。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

IsAsync 属性检测文件句柄是否已异步打开。The IsAsync property detects whether the file handle was opened asynchronously. 使用具有 isAsyncuseAsyncoptions 参数的构造函数创建 FileStream 类的实例时,可以指定此值。You specify this value when you create an instance of the FileStream class using a constructor that has an isAsync, useAsync, or options parameter. 如果 true属性,则流将使用重叠的 i/o 以异步方式执行文件操作。When the property is true, the stream utilizes overlapped I/O to perform file operations asynchronously. 不过,不必 true IsAsync 属性来调用 ReadAsyncWriteAsyncCopyToAsync 方法。However, the IsAsync property does not have to be true to call the ReadAsync, WriteAsync, or CopyToAsync method. false IsAsync 属性并且调用异步读写操作时,UI 线程仍不会被阻止,但实际的 i/o 操作将以同步方式执行。When the IsAsync property is false and you call the asynchronous read and write operations, the UI thread is still not blocked, but the actual I/O operation is performed synchronously.

Seek 方法支持对文件进行随机访问。The Seek method supports random access to files. Seek 允许将读/写位置移动到文件中的任何位置。Seek allows the read/write position to be moved to any position within the file. 这是通过字节偏移量引用点参数实现的。This is done with byte offset reference point parameters. 字节偏移量是相对于查找引用点的,它可以是基础文件的开头、当前位置或末尾,由 SeekOrigin 枚举的三个成员表示。The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three members of the SeekOrigin enumeration.

备注

磁盘文件始终支持随机访问。Disk files always support random access. 构造时,CanSeek 属性值设置为 truefalse 具体取决于基础文件类型。如果基础文件类型是 FILE_TYPE_DISK (如 winbase.h 中所定义),则 CanSeek 属性值 trueAt the time of construction, the CanSeek property value is set to true or false depending on the underlying file type.If the underlying file type is FILE_TYPE_DISK, as defined in winbase.h, the CanSeek property value is true. 否则,将 falseCanSeek 属性值。Otherwise, the CanSeek property value is false.

如果进程以锁定的部分文件终止或关闭具有未完成锁定的文件,则该行为是不确定的。If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.

有关目录操作和其他文件操作,请参阅 FileDirectoryPath 类。For directory operations and other file operations, see the File, Directory, and Path classes. File 类是一个实用工具类,该类具有用于基于文件路径创建 FileStream 对象的静态方法。The File class is a utility class that has static methods primarily for the creation of FileStream objects based on file paths. MemoryStream 类从字节数组创建流,与 FileStream 类类似。The MemoryStream class creates a stream from a byte array and is similar to the FileStream class.

有关常见文件和目录操作的列表,请参阅常见 I/o 任务For a list of common file and directory operations, see Common I/O Tasks.

检测流位置更改Detection of Stream Position Changes

FileStream 对象在其句柄上没有独占保留时,另一个线程可以同时访问文件句柄,并更改与文件句柄关联的操作系统文件指针的位置。When a FileStream object does not have an exclusive hold on its handle, another thread could access the file handle concurrently and change the position of the operating system's file pointer that is associated with the file handle. 在这种情况下,FileStream 对象中缓存的位置和缓冲区中的缓存数据可能会泄露。In this case, the cached position in the FileStream object and the cached data in the buffer could be compromised. FileStream 对象定期对访问缓存的缓冲区的方法执行检查,以确保操作系统的句柄位置与 FileStream 对象使用的缓存位置相同。The FileStream object routinely performs checks on methods that access the cached buffer to ensure that the operating system's handle position is the same as the cached position used by the FileStream object.

如果在对 Read 方法的调用中检测到句柄位置发生意外更改,则 .NET Framework 会丢弃缓冲区的内容,并再次从文件读取流。If an unexpected change in the handle position is detected in a call to the Read method, the .NET Framework discards the contents of the buffer and reads the stream from the file again. 这可能会影响性能,具体取决于文件的大小和可能影响文件流位置的任何其他进程。This can affect performance, depending on the size of the file and any other processes that could affect the position of the file stream.

如果在对 Write 方法的调用中检测到句柄位置发生意外更改,则会丢弃缓冲区的内容并引发 IOException 异常。If an unexpected change in the handle position is detected in a call to the Write method, the contents of the buffer are discarded and an IOException exception is thrown.

当访问 SafeFileHandle 属性以公开句柄或将 FileStream 对象提供在其构造函数中的 SafeFileHandle 属性时,FileStream 对象将不会在其句柄上独占保留。A FileStream object will not have an exclusive hold on its handle when either the SafeFileHandle property is accessed to expose the handle or the FileStream object is given the SafeFileHandle property in its constructor.

构造函数

FileStream(IntPtr, FileAccess)

使用指定的读/写权限为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.

FileStream(IntPtr, FileAccess, Boolean)

使用指定的读/写权限和 FileStream 实例所属权为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission and FileStream instance ownership.

FileStream(IntPtr, FileAccess, Boolean, Int32)

使用指定的读/写权限、FileStream 实例所属权和缓冲区大小为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, and buffer size.

FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)

使用指定的读/写权限、FileStream 实例所属权、缓冲区大小和同步或异步状态为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, buffer size, and synchronous or asynchronous state.

FileStream(SafeFileHandle, FileAccess)

使用指定的读/写权限为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.

FileStream(SafeFileHandle, FileAccess, Int32)

使用指定的读/写权限和缓冲区大小为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, and buffer size.

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

使用指定的读/写权限、缓冲区大小和同步或异步状态为指定的文件句柄初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, buffer size, and synchronous or asynchronous state.

FileStream(String, FileMode)

使用指定的路径和创建模式初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path and creation mode.

FileStream(String, FileMode, FileAccess)

使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.

FileStream(String, FileMode, FileAccess, FileShare)

使用指定的路径、创建模式、读/写权限和共享权限创建 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, read/write permission, and sharing permission.

FileStream(String, FileMode, FileAccess, FileShare, Int32)

用指定的路径、创建模式、读/写及共享权限和缓冲区大小初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, and buffer size.

FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)

使用指定的路径、创建模式、读/写和共享权限、缓冲区大小和同步或异步状态初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, buffer size, and synchronous or asynchronous state.

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

使用指定的路径、创建模式、读/写和共享权限、其他 FileStreams 可以具有的对此文件的访问权限、缓冲区大小和附加文件选项初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

使用指定的路径、创建模式、访问权限和共享权限、缓冲区大小和附加文件选项初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, and additional file options.

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

使用指定的路径、创建模式、访问权限和共享权限、缓冲区大小、附加文件选项、访问控制和审核安全初始化 FileStream 类的新实例。Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.

属性

CanRead

获取一个值,该值指示当前流是否支持读取。Gets a value that indicates whether the current stream supports reading.

CanSeek

获取一个值,该值指示当前流是否支持查找。Gets a value that indicates whether the current stream supports seeking.

CanTimeout

获取一个值,该值确定当前流是否可以超时。Gets a value that determines whether the current stream can time out.

(继承自 Stream)
CanWrite

获取一个值,该值指示当前流是否支持写入。Gets a value that indicates whether the current stream supports writing.

Handle

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

IsAsync

获取一个值,它指示 FileStream 是异步打开还是同步打开的。Gets a value that indicates whether the FileStream was opened asynchronously or synchronously.

Length

获取流的长度(以字节为单位)。Gets the length in bytes of the stream.

Name

获取 FileStream 中已打开的文件的绝对路径。Gets the absolute path of the file opened in the FileStream.

Position

获取或设置此流的当前位置。Gets or sets the current position of this stream.

ReadTimeout

获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取多长时间。Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

(继承自 Stream)
SafeFileHandle

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

WriteTimeout

获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试写入多长时间。Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

(继承自 Stream)

方法

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

开始异步读操作。Begins an asynchronous read operation. 请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

开始异步写操作。Begins an asynchronous write operation. 请考虑改用 WriteAsync(Byte[], Int32, Int32, CancellationToken)Consider using WriteAsync(Byte[], Int32, Int32, CancellationToken) instead.

Close()

关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)。Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.

CopyTo(Stream)

从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream.

(继承自 Stream)
CopyTo(Stream, Int32)

使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(继承自 Stream)
CopyToAsync(Stream)

从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream.

(继承自 Stream)
CopyToAsync(Stream, CancellationToken)

通过指定的取消令牌,从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

(继承自 Stream)
CopyToAsync(Stream, Int32)

使用指定的缓冲区大小,从当前流中异步读取字节并将其写入到另一流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(继承自 Stream)
CopyToAsync(Stream, Int32, CancellationToken)

使用指定的缓冲区大小和取消令牌,从当前文件流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current file stream and writes them to another stream, using a specified buffer size and cancellation token.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
CreateWaitHandle()

分配 WaitHandle 对象。Allocates a WaitHandle object.

(继承自 Stream)
Dispose()

释放由 Stream 使用的所有资源。Releases all resources used by the Stream.

(继承自 Stream)
Dispose(Boolean)

释放由 FileStream 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the FileStream and optionally releases the managed resources.

DisposeAsync()

异步释放 FileStream 使用的非托管资源。Asynchronously releases the unmanaged resources used by the FileStream.

EndRead(IAsyncResult)

等待挂起的异步读操作完成。Waits for the pending asynchronous read operation to complete. (请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。)(Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

EndWrite(IAsyncResult)

结束异步写入操作,在 I/O 操作完成之前一直阻止。Ends an asynchronous write operation and blocks until the I/O operation is complete. (请考虑改用 WriteAsync(Byte[], Int32, Int32, CancellationToken)。)(Consider using WriteAsync(Byte[], Int32, Int32, CancellationToken) instead.)

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

确保垃圾回收器回收 FileStream 时释放资源并执行其他清理操作。Ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the FileStream.

Flush()

清除此流的缓冲区,使得所有缓冲数据都写入到文件中。Clears buffers for this stream and causes any buffered data to be written to the file.

Flush(Boolean)

清除此流的缓冲区,将所有缓冲数据都写入到文件中,并且也清除所有中间文件缓冲区。Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.

FlushAsync()

异步清除此流的所有缓冲区并导致所有缓冲数据都写入基础设备中。Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.

(继承自 Stream)
FlushAsync(CancellationToken)

异步清理这个流的所有缓冲区,并使所有缓冲数据写入基础设备,并且监控取消请求。Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

GetAccessControl()

获取 FileSecurity 对象,该对象封装当前 FileStream 对象所描述的文件的访问控制列表 (ACL) 项。Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileStream object.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
Lock(Int64, Int64)

防止其他进程读取或写入 FileStreamPrevents other processes from reading from or writing to the FileStream.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
ObjectInvariant()

提供对 Contract 的支持。Provides support for a Contract.

(继承自 Stream)
Read(Byte[], Int32, Int32)

从流中读取字节块并将该数据写入给定缓冲区中。Reads a block of bytes from the stream and writes the data in a given buffer.

Read(Span<Byte>)

从当前文件流中读取字节序列,并在该文件流中按照读取的字节数提升位置。Reads a sequence of bytes from the current file stream and advances the position within the file stream by the number of bytes read.

ReadAsync(Byte[], Int32, Int32)

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

(继承自 Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

从当前文件流异步读取字节的序列,将其写入从指定偏移量开始的字节数组,按读取的字节数向前移动文件流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current file stream and writes them to a byte array beginning at a specified offset, advances the position within the file stream by the number of bytes read, and monitors cancellation requests.

ReadAsync(Memory<Byte>, CancellationToken)

从当前文件流异步读取字节的序列,将其写入某内存区域,按读取的字节数向前移动文件流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current file stream and writes them to a memory region, advances the position within the file stream by the number of bytes read, and monitors cancellation requests.

ReadByte()

从文件中读取一个字节,并将读取位置提升一个字节。Reads a byte from the file and advances the read position one byte.

Seek(Int64, SeekOrigin)

将该流的当前位置设置为给定值。Sets the current position of this stream to the given value.

SetAccessControl(FileSecurity)

FileSecurity 对象所描述的访问控制列表 (ACL) 项应用于当前 FileStream 对象所描述的文件。Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileStream object.

SetLength(Int64)

将该流的长度设置为给定值。Sets the length of this stream to the given value.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
Unlock(Int64, Int64)

允许其他进程访问以前锁定的某个文件的全部或部分。Allows access by other processes to all or part of a file that was previously locked.

Write(Byte[], Int32, Int32)

将字节块写入文件流。Writes a block of bytes to the file stream.

Write(ReadOnlySpan<Byte>)

将字节的序列从只读范围写入当前文件流,并按写入的字节数向前移动此文件流中的当前位置。Writes a sequence of bytes from a read-only span to the current file stream and advances the current position within this file stream by the number of bytes written.

WriteAsync(Byte[], Int32, Int32)

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

(继承自 Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

将字节的序列从内存区域异步写入当前文件流,按写入的字节数向前移动该文件流中的当前位置,并监视取消请求。Asynchronously writes a sequence of bytes from a memory region to the current file stream, advances the current position within this file stream by the number of bytes written, and monitors cancellation requests.

WriteByte(Byte)

一个字节写入文件流中的当前位置。Writes a byte to the current position in the file stream.

显式界面实现

IDisposable.Dispose()

释放由 Stream 使用的所有资源。Releases all resources used by the Stream.

(继承自 Stream)

扩展方法

GetAccessControl(FileStream)
SetAccessControl(FileStream, FileSecurity)
AsInputStream(Stream)

将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输入流。Converts a managed stream in the .NET for Windows Store apps to an input stream in the Windows Runtime.

AsOutputStream(Stream)

将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输出流。Converts a managed stream in the .NET for Windows Store apps to an output stream in the Windows Runtime.

AsRandomAccessStream(Stream)

将指定的流转换为随机访问流。Converts the specified stream to a random access stream.

适用于

另请参阅