System.IO.UnmanagedMemoryStream.Write 方法

方法描述

使用缓冲区中的数据将字节块写入当前流。

语法定义(C# System.IO.UnmanagedMemoryStream.Write 方法 的用法)

public override void Write(
	byte[] buffer,
	int offset,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
buffer System-Byte[] 字节数组,从该字节数组将字节复制到当前流中。
offset System-Int32 缓冲区中的偏移量,从此处开始将字节复制到当前流中。
count System-Int32 要写入当前流的字节数。
返回值 void

提示和注释

在流的当前位置发生写操作。

System.IO.UnmanagedMemoryStream.Write 方法例子

使用 Marshal 类分配和解除分配非托管内存块。

// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter
{

    static void Main()
	{
		
            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");

	    // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.
            writeStream.Close();

            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);

	    // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.
            readStream.Close();

            // Display the data to the console.
            Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));

            // Free the block of unmanaged memory.
            Marshal.FreeHGlobal(memIntPtr);

            Console.ReadLine();
    }
}

异常

异常 异常描述
ObjectDisposedException 流已关闭。
NotSupportedException
  • 基础内存不支持写入。
  • 尝试写入流,但 CanWrite 属性为 false。
  • count 值大于流的容量。
  • 位置在流容量的末尾。
IOException 发生 I/O 错误。
ArgumentOutOfRangeException 其中一个指定的参数小于 0。
ArgumentException offset 参数减去 buffer 参数的长度小于 count 参数。
ArgumentNullException buffer 参数为 null。

命名空间

namespace: System.IO

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.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 系统要求。