Stream 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供字节序列的一般视图。 这是一个抽象类。
public ref class Stream abstract : IDisposable
public ref class Stream abstract : MarshalByRefObject, IAsyncDisposable, IDisposable
public ref class Stream abstract : MarshalByRefObject, IDisposable
public abstract class Stream : IDisposable
public abstract class Stream : MarshalByRefObject, IAsyncDisposable, IDisposable
public abstract class Stream : MarshalByRefObject, IDisposable
[System.Serializable]
public abstract class Stream : MarshalByRefObject, IDisposable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Stream : MarshalByRefObject, IDisposable
type Stream = class
interface IDisposable
type Stream = class
inherit MarshalByRefObject
interface IAsyncDisposable
interface IDisposable
type Stream = class
inherit MarshalByRefObject
interface IDisposable
[<System.Serializable>]
type Stream = class
inherit MarshalByRefObject
interface IDisposable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Stream = class
inherit MarshalByRefObject
interface IDisposable
Public MustInherit Class Stream
Implements IDisposable
Public MustInherit Class Stream
Inherits MarshalByRefObject
Implements IAsyncDisposable, IDisposable
Public MustInherit Class Stream
Inherits MarshalByRefObject
Implements IDisposable
- 继承
-
Stream
- 继承
- 派生
- 属性
- 实现
示例
以下示例演示如何使用两个 FileStream 对象以异步方式将文件从一个目录复制到另一个目录。 FileStream 类是从 Stream 类派生的。 需要注意 Click 控件的 Button 事件处理程序具有 async
修饰符标记,因为它调用异步方法。
using System;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
namespace WpfApplication
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
string StartDirectory = @"c:\Users\exampleuser\start";
string EndDirectory = @"c:\Users\exampleuser\end";
foreach (string filename in Directory.EnumerateFiles(StartDirectory))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
}
}
Imports System.IO
Class MainWindow
Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim StartDirectory As String = "c:\Users\exampleuser\start"
Dim EndDirectory As String = "c:\Users\exampleuser\end"
For Each filename As String In Directory.EnumerateFiles(StartDirectory)
Using SourceStream As FileStream = File.Open(filename, FileMode.Open)
Using DestinationStream As FileStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf("\"c)))
Await SourceStream.CopyToAsync(DestinationStream)
End Using
End Using
Next
End Sub
End Class
注解
Stream 是所有流的抽象基类。 流是字节序列的抽象,例如文件、输入/输出设备、进程中通信管道或 TCP/IP 套接字。 类 Stream 及其派生类提供这些不同类型的输入和输出的通用视图,并将程序员与操作系统和基础设备的特定详细信息隔离开来。
流涉及三个基本操作:
可以从流中读取数据。 读取是将数据从流传输到数据结构中,例如字节数组。
可以写入流。 写入是将数据从数据结构传输到流中。
流可以支持查找。 查找是指查询和修改流中的当前位置。 搜寻功能取决于流具有的后备存储类型。 例如,网络流没有当前位置的统一概念,因此通常不支持查找。
继承自 Stream 的一些更常用的流包括 FileStream、 和 MemoryStream。
根据基础数据源或存储库,流可能仅支持其中某些功能。 可以使用 类的 、 CanWrite和 CanSeek 属性Stream查询流的功能CanRead。
Read和 Write 方法以各种格式读取和写入数据。 对于支持查找的流,请使用 Seek 和 SetLength 方法以及 Position 和 Length 属性来查询和修改流的当前位置和长度。
此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try
/catch
块中调用其 Dispose 方法。 若要间接释放类型,请使用 using
(在 C# 中)或 Using
(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。
释放 Stream 对象会刷新所有缓冲的数据,并实质上为你调用 Flush 方法。 Dispose 还会释放操作系统资源,例如文件句柄、网络连接或用于任何内部缓冲的内存。 类 BufferedStream 提供将缓冲流包装在另一个流周围的功能,以提高读取和写入性能。
从 .NET Framework 4.5 开始, Stream 类包括用于简化异步操作的异步方法。 异步方法 Async
的名称中包含 ,例如 ReadAsync、 WriteAsync、 CopyToAsync和 FlushAsync。 这些方法使你能够执行资源密集型 I/O 操作,而不会阻止main线程。 在 Windows 8.x 应用商店应用或桌面应用中一个耗时的流操作可能阻塞 UI 线程并让应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。 异步方法与 async
Visual Basic 和 C# 中的 和 await
关键字结合使用。
在 Windows 8.x 应用商店应用中使用时, Stream 包括两个扩展方法: AsInputStream 和 AsOutputStream。 这些方法将 Stream 对象转换为Windows 运行时中的流。 还可以使用 AsStreamForRead 和 AsStreamForWrite 方法将 Windows 运行时 Stream 中的流转换为 对象。 有关详细信息,请参阅如何:在.NET Framework流和Windows 运行时流之间转换
某些流实现对基础数据执行本地缓冲以提高性能。 对于此类流,可以使用 Flush 或 FlushAsync 方法来清除任何内部缓冲区,并确保所有数据都已写入基础数据源或存储库。
如果需要没有后备存储 (也称为位存储桶) 的流,请使用 Null 字段检索专为此目的设计的流的实例。
实施者说明
实现 的Stream派生类时,必须为 和 Write(Byte[], Int32, Int32) 方法提供实现Read(Byte[], Int32, Int32)。 异步方法 ReadAsync(Byte[], Int32, Int32)、 WriteAsync(Byte[], Int32, Int32)和 CopyToAsync(Stream) 在其实现中使用同步方法 Read(Byte[], Int32, Int32) 和 Write(Byte[], Int32, Int32) 。 因此,和 Write(Byte[], Int32, Int32) 的Read(Byte[], Int32, Int32)实现将正确使用异步方法。 和 的默认实现ReadByte()创建一个新的单元素字节数组,然后调用 和 Write(Byte[], Int32, Int32)的Read(Byte[], Int32, Int32)实现。WriteByte(Byte) 从 Stream派生时,建议重写这些方法来访问内部缓冲区(如果有),以便大幅提高性能。 还必须提供 、、、 Flush()Seek(Int64, SeekOrigin)CanSeekCanWriteLengthPosition和 SetLength(Int64)的CanRead实现。
不要重写 Close() 方法,而是将所有 Stream 清理逻辑放在 方法中 Dispose(Boolean) 。 有关详细信息,请参阅 实现 Dispose 方法。
构造函数
Stream() |
初始化 Stream 类的新实例。 |
字段
Null |
无后备存储区的 |
属性
CanRead |
当在派生类中重写时,获取指示当前流是否支持读取的值。 |
CanSeek |
当在派生类中重写时,获取指示当前流是否支持查找功能的值。 |
CanTimeout |
获取一个值,该值确定当前流是否可以超时。 |
CanWrite |
当在派生类中重写时,获取指示当前流是否支持写入功能的值。 |
Length |
当在派生类中重写时,获取流长度(以字节为单位)。 |
Position |
当在派生类中重写时,获取或设置当前流中的位置。 |
ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前将尝试读取的时间。 |
WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前将尝试写入多长时间。 |
方法
显式接口实现
IDisposable.Dispose() |
释放由 Stream 使用的所有资源。 |
扩展方法
AsInputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输入流。 |
AsOutputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输出流。 |
AsRandomAccessStream(Stream) |
将指定的流转换为随机访问流。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |