System.IO.MemoryStream 类

方法描述

创建其支持存储区为内存的流。

语法定义(C# System.IO.MemoryStream 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class MemoryStream : Stream

构造函数

构造函数名称 构造函数描述
MemoryStream() 使用初始化为零的可扩展容量初始化 MemoryStream 类的新实例。
MemoryStream(Byte[]) 基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。
MemoryStream(Int32) 使用按指定要求初始化的可扩展容量初始化 MemoryStream 类的新实例。
MemoryStream(Byte[], Boolean) 使用按指定要求设置的 CanWrite 属性基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。
MemoryStream(Byte[], Int32, Int32) 基于字节数组的指定区域(索引)初始化 MemoryStream 类的无法调整大小的新实例。
MemoryStream(Byte[], Int32, Int32, Boolean) 使用按指定要求设置的 CanWrite 属性基于字节数组的指定区域初始化 MemoryStream 类的无法调整大小的新实例。
MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) 在按指定要求设置 CanWrite 属性而且能够调用按指定要求设置的 GetBuffer 的情况下,基于字节数组的指定区域初始化 MemoryStream 类的新实例。

成员/方法

方法名称 方法描述
BeginRead 开始异步读操作。 (继承自 Stream。)
BeginWrite 开始异步写操作。 (继承自 Stream。)
Close 关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)。 (继承自 Stream。)
CopyTo(Stream) 从当前流中读取字节并将其写入到目标流中。 (继承自 Stream。)
CopyTo(Stream, Int32) 从当前流中读取所有字节并将其写入到目标流中(使用指定的缓冲区大小)。 (继承自 Stream。)
CreateObjRef 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (继承自 MarshalByRefObject。)
CreateWaitHandle 已过时。分配 WaitHandle 对象。 (继承自 Stream。)
Dispose() 释放由 Stream 占用的所有资源。 (继承自 Stream。)
Dispose(Boolean) 释放 MemoryStream 类使用的非托管资源,并可以选择释放托管资源。 (重写 Stream.Dispose(Boolean)。)
EndRead 等待挂起的异步读取完成。 (继承自 Stream。)
EndWrite 结束异步写操作。 (继承自 Stream。)
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
Flush 重写 Stream.Flush 以便不执行任何操作。 (重写 Stream.Flush()。)
GetBuffer 返回从其创建此流的无符号字节数组。
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetLifetimeService 检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject。)
GetType 获取当前实例的 Type。 (继承自 Object。)
InitializeLifetimeService 获取控制此实例的生存期策略的生存期服务对象。 (继承自 MarshalByRefObject。)
MemberwiseClone() 创建当前 Object 的浅表副本。 (继承自 Object。)
MemberwiseClone(Boolean) 创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject。)
ObjectInvariant 基础结构。提供对 Contract 的支持。 (重写 Stream.ObjectInvariant()。)
Read 从当前流中读取字节块并将数据写入缓冲区中。 (重写 Stream.Read(Byte[], Int32, Int32)。)
ReadByte 从当前流中读取一个字节。 (重写 Stream.ReadByte()。)
Seek 将当前流中的位置设置为指定值。 (重写 Stream.Seek(Int64, SeekOrigin)。)
SetLength 将当前流的长度设为指定值。 (重写 Stream.SetLength(Int64)。)
ToArray 将流内容写入字节数组,而与 Position 属性无关。
ToString 返回表示当前对象的字符串。 (继承自 Object。)
Write 使用从某个缓冲区读取的数据将字节块写入当前流。 (重写 Stream.Write(Byte[], Int32, Int32)。)
WriteByte 将一个字节写入当前流中的当前位置。 (重写 Stream.WriteByte(Byte)。)
WriteTo 将此内存流的整个内容写入另一个流中。

提示和注释

流的当前位置是下一个读取或写入操作可能发生的位置。 当前位置可以通过 Seek 方法检索或设置。 在创建 MemoryStream 的新实例时,当前位置设置为零。

用无符号字节数组创建的内存流提供无法调整大小的数据流。 当使用字节数组时,虽然根据传递到构造函数中的参数可能能够修改现有内容,但既不能追加也不能收缩流。 空内存流是可调整大小的,而且可以向其写入和从中读取。

如果将 MemoryStream 对象添加到 ResX 文件或 .resources 文件中,则可在运行时调用 GetStream 方法对其进行检索。

如果将 MemoryStream 对象序列化为资源文件,它将被实际序列化为 UnmanagedMemoryStream。 此行为可以提供更好的性能,并可以提供将指针直接指向数据的功能,而不必使用 Stream 方法。

System.IO.MemoryStream 类例子

下面的代码示例说明如何通过将内存用作备份来读取和写入数据。

using System;
using System.IO;
using System.Text;

class MemStream
{
    static void Main()
    {
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();

        // Create the data to write to the stream.
        byte[] firstString = uniEncoding.GetBytes(
            "Invalid file path characters are: ");
        byte[] secondString = uniEncoding.GetBytes(
            Path.GetInvalidPathChars());

        using(MemoryStream memStream = new MemoryStream(100))
        {
            // Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length);

            // Write the second string to the stream, byte by byte.
            count = 0;
            while(count < secondString.Length)
            {
                memStream.WriteByte(secondString[count++]);
            }

            // Write the stream properties to the console.
            Console.WriteLine(
                "Capacity = {0}, Length = {1}, Position = {2}\n",
                memStream.Capacity.ToString(),
                memStream.Length.ToString(),
                memStream.Position.ToString());

            // Set the position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin);

            // Read the first 20 bytes from the stream.
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);

            // Read the remaining bytes, byte by byte.
            while(count < memStream.Length)
            {
                byteArray[count++] =
                    Convert.ToByte(memStream.ReadByte());
            }

            // Decode the byte array into a char array
            // and write it to the console.
            charArray = new char[uniEncoding.GetCharCount(
                byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(
                byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray);
        }
    }
}

继承层次结构

System.Object

System.MarshalByRefObject

System.IO.Stream

System.IO.MemoryStream

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。