UnmanagedMemoryStream.Write 方法

定義

多載

Write(ReadOnlySpan<Byte>)

使用所提供位元組範圍中的資料,將位元組區塊寫入至目前的非受控記憶體資料流。

Write(Byte[], Int32, Int32)

使用緩衝區的資料,將位元組區塊寫入目前的資料流。

Write(ReadOnlySpan<Byte>)

來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
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> -> unit
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (source As ReadOnlySpan(Of Byte))
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

參數

sourcebuffer
ReadOnlySpan<Byte>

要從中將位元組複製到目前非受控記憶體資料流的位元組範圍。

適用於

Write(Byte[], Int32, Int32)

來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
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 -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)

參數

buffer
Byte[]

從中複製位元組至目前資料流的位元組陣列。

offset
Int32

在此要開始複製位元組到目前資料流的緩衝區中位移。

count
Int32

要寫入目前資料流的位元組數目。

例外狀況

資料流已關閉。

基礎記憶體不支援寫入。

-或-

嘗試進行資料流寫入,且 CanWrite 屬性為 false

-或-

count 值大於資料流容量。

-或-

位置位於資料流容量結尾。

發生 I/O 錯誤。

其中一個指定的參數小於零。

offset 參數減去 buffer 參數的長度小於 count 參數。

buffer 參數為 null

範例

下列程式碼範例示範如何使用 類別讀取和寫入 Unmanaged 記憶體 UnmanagedMemoryStream 。 Unmanaged 記憶體區塊會使用 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();
    }
}

備註

寫入會在資料流程的目前位置發生。

適用於