UnmanagedMemoryStream.Write 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| Write(ReadOnlySpan<Byte>) | 使用来自提供的字节跨度的数据将字节块写入当前非管理的内存流。 | 
| Write(Byte[], Int32, Int32) | 使用缓冲区中的数据将字节块写入当前流。 | 
Write(ReadOnlySpan<Byte>)
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
使用来自提供的字节跨度的数据将字节块写入当前非管理的内存流。
public:
 override void Write(ReadOnlySpan<System::Byte> source);public:
 override void Write(ReadOnlySpan<System::Byte> buffer);public override void Write (ReadOnlySpan<byte> source);public override void Write (ReadOnlySpan<byte> buffer);override this.Write : ReadOnlySpan<byte> -> unitoverride this.Write : ReadOnlySpan<byte> -> unitPublic Overrides Sub Write (source As ReadOnlySpan(Of Byte))Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))参数
- sourcebuffer
- ReadOnlySpan<Byte>
将字节复制到当前非管理的内存流的字节跨度。
适用于
Write(Byte[], Int32, Int32)
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
使用缓冲区中的数据将字节块写入当前流。
public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);public override void Write (byte[] buffer, int offset, int count);override this.Write : byte[] * int * int -> unitPublic Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)参数
- buffer
- Byte[]
字节数组,从该字节数组将字节复制到当前流中。
- offset
- Int32
缓冲区中的偏移量,从此处开始将字节复制到当前流中。
- count
- Int32
要写入当前流的字节数。
例外
流已关闭。
出现 I/O 错误。
其中一个指定的参数小于 0。
              offset 参数减去 buffer 参数的长度小于 count 参数。
              buffer 参数为 null。
示例
下面的代码示例演示如何使用 UnmanagedMemoryStream 类读取和写入非托管内存。 使用 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();
    }
}
注解
写入发生在流中的当前位置。