UnmanagedMemoryStream.WriteByte(Byte) UnmanagedMemoryStream.WriteByte(Byte) UnmanagedMemoryStream.WriteByte(Byte) UnmanagedMemoryStream.WriteByte(Byte) Method

定義

寫入一個位元組到檔案資料流中的目前位置。Writes a byte to the current position in the file stream.

public:
 override void WriteByte(System::Byte value);
public override void WriteByte (byte value);
override this.WriteByte : byte -> unit
Public Overrides Sub WriteByte (value As Byte)

參數

value
Byte Byte Byte Byte

寫入資料流的位元組值。A byte value written to the stream.

例外狀況

基礎記憶體不支援寫入。The underlying memory does not support writing.

-或--or- 嘗試進行資料流寫入,且 CanWrite 屬性為 falseAn attempt is made to write to the stream and the CanWrite property is false.

-或--or- 目前位置位於資料流容量的結尾。The current position is at the end of the capacity of the stream.

所提供的 value 導致資料流超出最大容量。The supplied value causes the stream exceed its maximum capacity.

範例

下列程式碼範例示範如何使用UnmanagedMemoryStream類別來讀取和寫入非受控記憶體。The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. 使用Marshal類別配置和解除配置非受控記憶體的區塊。A block of unmanaged memory is allocated and de-allocated using the Marshal class. 在此範例中, UnmanagedMemoryStream物件會在嘗試將資料寫入資料流程之前CanWrite , 傳遞至檢查屬性的方法。In this example, an UnmanagedMemoryStream object is passed to a method which checks the CanWrite property before attempting to write the data to the stream.

// 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.Runtime.InteropServices;
using System.Text;

unsafe class Program
{
    static void Main()
    {
        // Create some data to write.
        byte[] text = UnicodeEncoding.Unicode.GetBytes("Data to write.");

        // Allocate a block of unmanaged memory.
        IntPtr memIntPtr = Marshal.AllocHGlobal(text.Length);

        // Get a byte pointer from the unmanaged memory block.
        byte* memBytePtr = (byte*)memIntPtr.ToPointer();

        UnmanagedMemoryStream writeStream =
            new UnmanagedMemoryStream(
            memBytePtr, text.Length, text.Length, FileAccess.Write);

        // Write the data.
        WriteToStream(writeStream, text);

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

        // Create another UnmanagedMemoryStream for reading.
        UnmanagedMemoryStream readStream =
            new UnmanagedMemoryStream(memBytePtr, text.Length);

        // Display the contents of the stream to the console.
        PrintStream(readStream);

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

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

    }

    public static void WriteToStream(UnmanagedMemoryStream writeStream, byte[] text)
    {
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (writeStream.CanWrite)
        {
            // Write the data, byte by byte
            for (int i = 0; i < writeStream.Length; i++)
            {
                writeStream.WriteByte(text[i]);
            }
        }
    }

    public static void PrintStream(UnmanagedMemoryStream readStream)
    {
        byte[] text = new byte[readStream.Length];
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (readStream.CanRead)
        {
            // Write the data, byte by byte
            for (int i = 0; i < readStream.Length; i++)
            {
                text[i] = (byte)readStream.ReadByte();
            }
        }

        Console.WriteLine(UnicodeEncoding.Unicode.GetString(text));
    }
}

適用於