BinaryWriter.Write 方法

定義

寫入一個值到目前資料流。

多載

Write(Char[], Int32, Int32)

將字元陣列中的一區段寫入至目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。

Write(Byte[], Int32, Int32)

將一個區域的位元組陣列寫入目前資料流。

Write(UInt64)

將八位元組不帶正負號的整數值寫入目前資料流,並將資料流目前位置前移八個位元組。

Write(UInt32)

將四位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。

Write(UInt16)

將二位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移兩個位元組。

Write(String)

將有長度前置字元的字串以目前 BinaryWriter 的編碼方式寫入此資料流,並按照所使用的編碼方式和寫入資料流的特定字元,將資料流中目前的位置往前移。

Write(Single)

將四位元組浮點數 (Floating-Point) 值寫入目前資料流,並將資料流目前位置前移四個位元組。

Write(SByte)

將帶正負號 (Signed) 位元組寫入目前資料流,並將資料流位置前移一個位元組。

Write(ReadOnlySpan<Char>)

將字元範圍寫入目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。

Write(ReadOnlySpan<Byte>)

將位元組範圍寫入目前的資料流。

Write(Int64)

將八位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移八個位元組。

Write(Char[])

將字元陣列寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。

Write(Int16)

將二位元組帶正負號的整數 (Signed Integer) 寫入目前資料流,並將資料流目前位置前移兩個位元組。

Write(Half)

將雙位元組浮點數寫入目前的數據流,並將數據流位置往前移兩個字節。

Write(Double)

將八位元組浮點數值寫入目前資料流,並將資料流目前位置前移八個位元組。

Write(Decimal)

將十進位值寫入目前的資料流,並將資料流位置往前移十六個位元組。

Write(Char)

將 Unicode 字元寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。

Write(Byte[])

將位元組陣列寫入基礎資料流。

Write(Byte)

將不帶正負號 (Unsigned) 位元組寫入目前資料流,並將資料流位置前移一個位元組。

Write(Boolean)

寫入一位元組的 Boolean 值至目前資料流,其中 0 表示 false,1 表示 true

Write(Int32)

將四位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。

Write(Char[], Int32, Int32)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將字元陣列中的一區段寫入至目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。

C#
public virtual void Write (char[] chars, int index, int count);

參數

chars
Char[]

含有要寫入之資料的字元陣列。

index
Int32

要從 chars 讀取並寫入到資料流之第一個字元的索引。

count
Int32

要從 chars 讀取並寫入到資料流的字元數。

例外狀況

緩衝區長度減去 index 會小於 count

charsnull

indexcount 為負。

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來讀取和寫入數據。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        // Create the reader using the same MemoryStream
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        int arraySize = (int)(memStream.Length - memStream.Position);
        char[] memoryData = new char[arraySize];
        binReader.Read(memoryData, 0, arraySize);
        Console.WriteLine(memoryData);
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Byte[], Int32, Int32)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將一個區域的位元組陣列寫入目前資料流。

C#
public virtual void Write (byte[] buffer, int index, int count);

參數

buffer
Byte[]

含有要寫入之資料的位元組陣列。

index
Int32

要從 buffer 讀取並寫入到資料流之第一個位元組的索引。

count
Int32

要從 buffer 讀取並寫入到資料流的位元組數目。

例外狀況

緩衝區長度減去 index 會小於 count

buffernull

indexcount 為負。

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來寫入二進位數據,然後確認數據已正確寫入。

C#
using System;
using System.IO;

namespace BinaryRW
{
    class Program
    {
        static void Main(string[] args)
        {
            const int arrayLength = 1000;
            byte[] dataArray = new byte[arrayLength];
            byte[] verifyArray = new byte[arrayLength];

            new Random().NextBytes(dataArray);

            using (BinaryWriter binWriter = new BinaryWriter(new MemoryStream()))
            {
                Console.WriteLine("Writing the data.");
                binWriter.Write(dataArray, 0, arrayLength);

                using (BinaryReader binReader = new BinaryReader(binWriter.BaseStream))
                {
                    binReader.BaseStream.Position = 0;

                    if (binReader.Read(verifyArray, 0, arrayLength) != arrayLength)
                    {
                        Console.WriteLine("Error writing the data.");
                        return;
                    }
                }
            }

            for (int i = 0; i < arrayLength; i++)
            {
                if (verifyArray[i] != dataArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }

            Console.WriteLine("The data was written and verified.");
        }
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(UInt64)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

重要

此 API 不符合 CLS 規範。

將八位元組不帶正負號的整數值寫入目前資料流,並將資料流目前位置前移八個位元組。

C#
[System.CLSCompliant(false)]
public virtual void Write (ulong value);

參數

value
UInt64

要寫入的八位元組不帶正負號的整數值。

屬性

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

BinaryWriter 以小到小格式儲存此數據類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(UInt32)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

重要

此 API 不符合 CLS 規範。

將四位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。

C#
[System.CLSCompliant(false)]
public virtual void Write (uint value);

參數

value
UInt32

要寫入的四位元組不帶正負號的整數。

屬性

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

BinaryWriter 以小到小格式儲存此數據類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(UInt16)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

重要

此 API 不符合 CLS 規範。

將二位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移兩個位元組。

C#
[System.CLSCompliant(false)]
public virtual void Write (ushort value);

參數

value
UInt16

要寫入的二位元組不帶正負號的整數。

屬性

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

BinaryWriter 以小到小格式儲存此數據類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(String)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將有長度前置字元的字串以目前 BinaryWriter 的編碼方式寫入此資料流,並按照所使用的編碼方式和寫入資料流的特定字元,將資料流中目前的位置往前移。

C#
public virtual void Write (string value);

參數

value
String

要寫入的值。

例外狀況

發生 I/O 錯誤。

valuenull

資料流已關閉。

範例

下列程式代碼範例示範如何在檔案中儲存和擷取應用程式設定。

C#
using System;
using System.IO;
using System.Text;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                writer.Write(1.250F);
                writer.Write(@"c:\Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

備註

長度前置詞表示此方法會先將字串的長度以位元組為單位,以位元組為單位,以 BinaryWriter 實例目前的編碼方式編碼至數據流。 這個值會寫入為不帶正負號的整數。 這個方法接著會將許多位元組寫入數據流。

例如,字串 「A」 的長度為 1,但在以 UTF-16 編碼時;長度為 2 個字節,因此以前置詞寫入的值是 2,而 3 個字節會寫入數據流,包括前置詞。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Single)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將四位元組浮點數 (Floating-Point) 值寫入目前資料流,並將資料流目前位置前移四個位元組。

C#
public virtual void Write (float value);

參數

value
Single

要寫入的四位元組浮點數值。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何在檔案中儲存和擷取應用程式設定。

C#
using System;
using System.IO;
using System.Text;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                writer.Write(1.250F);
                writer.Write(@"c:\Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

備註

BinaryWriter 以小到小格式儲存此數據類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(SByte)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

重要

此 API 不符合 CLS 規範。

將帶正負號 (Signed) 位元組寫入目前資料流,並將資料流位置前移一個位元組。

C#
[System.CLSCompliant(false)]
public virtual void Write (sbyte value);

參數

value
SByte

要寫入的帶正負號位元組。

屬性

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(ReadOnlySpan<Char>)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將字元範圍寫入目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。

C#
public virtual void Write (ReadOnlySpan<char> chars);

參數

chars
ReadOnlySpan<Char>

要寫入的字元範圍。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Write(ReadOnlySpan<Byte>)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將位元組範圍寫入目前的資料流。

C#
public virtual void Write (ReadOnlySpan<byte> buffer);

參數

buffer
ReadOnlySpan<Byte>

要寫入的位元組範圍。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Write(Int64)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將八位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移八個位元組。

C#
public virtual void Write (long value);

參數

value
Int64

要寫入的八位元組帶正負號的整數。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

BinaryWriter 以小到小格式儲存此數據類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Char[])

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將字元陣列寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。

C#
public virtual void Write (char[] chars);

參數

chars
Char[]

含有要寫入之資料的字元陣列。

例外狀況

charsnull

資料流已關閉。

發生 I/O 錯誤。

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來讀取和寫入數據。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(Path.InvalidPathChars);

        // Create the reader using the same MemoryStream
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        Console.WriteLine(binReader.ReadChars(
            (int)(memStream.Length - memStream.Position)));
    }
}

備註

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Int16)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將二位元組帶正負號的整數 (Signed Integer) 寫入目前資料流,並將資料流目前位置前移兩個位元組。

C#
public virtual void Write (short value);

參數

value
Int16

要寫入的二位元組帶正負號的整數值。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

BinaryWriter 以小到小格式儲存此數據類型。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Half)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將雙位元組浮點數寫入目前的數據流,並將數據流位置往前移兩個字節。

C#
public virtual void Write (Half value);

參數

value
Half

要寫入的雙位元組浮點數。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何使用 類別頂端MemoryStreamBinaryReaderBinaryWriter 類別,將數據讀取和寫入Double記憶體。 MemoryStream 只會讀取和寫入 Byte 數據。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i;
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        Random randomGenerator = new Random();
        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
        {
            dataArray[i] = 100.1 * randomGenerator.NextDouble();
        }

        using(BinaryWriter binWriter =
            new BinaryWriter(new MemoryStream()))
        {
            // Write the data to the stream.
            Console.WriteLine("Writing data to the stream.");
            for(i = 0; i < arrayLength; i++)
            {
                binWriter.Write(dataArray[i]);
            }

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader =
                new BinaryReader(binWriter.BaseStream))
            {
                try
                {
                    // Return to the beginning of the stream.
                    binReader.BaseStream.Position = 0;

                    // Read and verify the data.
                    Console.WriteLine("Verifying the written data.");
                    for(i = 0; i < arrayLength; i++)
                    {
                        if(binReader.ReadDouble() != dataArray[i])
                        {
                            Console.WriteLine("Error writing data.");
                            break;
                        }
                    }
                    Console.WriteLine("The data was written " +
                        "and verified.");
                }
                catch(EndOfStreamException e)
                {
                    Console.WriteLine("Error writing data: {0}.",
                        e.GetType().Name);
                }
            }
        }
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9

Write(Double)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將八位元組浮點數值寫入目前資料流,並將資料流目前位置前移八個位元組。

C#
public virtual void Write (double value);

參數

value
Double

要寫入的八位元組浮點數值。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何使用 類別頂端MemoryStreamBinaryReaderBinaryWriter 類別,將數據讀取和寫入Double記憶體。 MemoryStream 只會讀取和寫入 Byte 數據。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i;
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        Random randomGenerator = new Random();
        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
        {
            dataArray[i] = 100.1 * randomGenerator.NextDouble();
        }

        using(BinaryWriter binWriter =
            new BinaryWriter(new MemoryStream()))
        {
            // Write the data to the stream.
            Console.WriteLine("Writing data to the stream.");
            for(i = 0; i < arrayLength; i++)
            {
                binWriter.Write(dataArray[i]);
            }

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader =
                new BinaryReader(binWriter.BaseStream))
            {
                try
                {
                    // Return to the beginning of the stream.
                    binReader.BaseStream.Position = 0;

                    // Read and verify the data.
                    Console.WriteLine("Verifying the written data.");
                    for(i = 0; i < arrayLength; i++)
                    {
                        if(binReader.ReadDouble() != dataArray[i])
                        {
                            Console.WriteLine("Error writing data.");
                            break;
                        }
                    }
                    Console.WriteLine("The data was written " +
                        "and verified.");
                }
                catch(EndOfStreamException e)
                {
                    Console.WriteLine("Error writing data: {0}.",
                        e.GetType().Name);
                }
            }
        }
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Decimal)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將十進位值寫入目前的資料流,並將資料流位置往前移十六個位元組。

C#
public virtual void Write (decimal value);

參數

value
Decimal

要寫入的十進位值。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

備註

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Char)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將 Unicode 字元寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。

C#
public virtual void Write (char ch);

參數

ch
Char

非 Surrogate,要寫入的 Unicode 字元。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

ch 是單一 Surrogate 字元。

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來讀取和寫入數據。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i = 0;
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        for(i = 0; i < invalidPathChars.Length; i++)
        {
            binWriter.Write(invalidPathChars[i]);
        }

        // Create the reader using the same MemoryStream
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        char[] memoryData =
            new char[memStream.Length - memStream.Position];
        for(i = 0; i < memoryData.Length; i++)
        {
            memoryData[i] = binReader.ReadChar();
        }
        Console.WriteLine(memoryData);
    }
}

備註

由於數據格式設定衝突,因此不建議搭配下列編碼方式使用此方法:

  • UTF-7

  • ISO-2022-JP

  • ISCII

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

Unicode 代理字元必須在相同的呼叫中以配對的形式寫出,而不是個別寫出。 如果您需要支援應用程式中的 Surrogate 字組,請考慮使用字元陣列和 Write 方法多載。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Byte[])

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將位元組陣列寫入基礎資料流。

C#
public virtual void Write (byte[] buffer);

參數

buffer
Byte[]

含有要寫入之資料的位元組陣列。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

buffernull

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來寫入二進位數據,然後確認數據已正確寫入。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        byte[] dataArray = new byte[arrayLength];
        new Random().NextBytes(dataArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());

        // Write the data to the stream.
        Console.WriteLine("Writing the data.");
        binWriter.Write(dataArray);

        // Create the reader using the stream from the writer.
        BinaryReader binReader =
            new BinaryReader(binWriter.BaseStream);

        // Set Position to the beginning of the stream.
        binReader.BaseStream.Position = 0;

        // Read and verify the data.
        byte[] verifyArray = binReader.ReadBytes(arrayLength);
        if(verifyArray.Length != arrayLength)
        {
            Console.WriteLine("Error writing the data.");
            return;
        }
        for(int i = 0; i < arrayLength; i++)
        {
            if(verifyArray[i] != dataArray[i])
            {
                Console.WriteLine("Error writing the data.");
                return;
            }
        }
        Console.WriteLine("The data was written and verified.");
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Byte)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將不帶正負號 (Unsigned) 位元組寫入目前資料流,並將資料流位置前移一個位元組。

C#
public virtual void Write (byte value);

參數

value
Byte

要寫入的不帶正負號位元組。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何使用記憶體作為備份儲存區來寫入二進位數據,然後確認數據已正確寫入。

C#
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i = 0;

        // Create random data to write to the stream.
        byte[] writeArray = new byte[1000];
        new Random().NextBytes(writeArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        BinaryReader binReader =
            new BinaryReader(binWriter.BaseStream);

        try
        {
            // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            for(i = 0; i < writeArray.Length; i++)
            {
                binWriter.Write(writeArray[i]);
            }

            // Set the stream position to the beginning of the stream.
            binReader.BaseStream.Position = 0;

            // Read and verify the data from the stream.
            for(i = 0; i < writeArray.Length; i++)
            {
                if(binReader.ReadByte() != writeArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
            Console.WriteLine("The data was written and verified.");
        }

        // Catch the EndOfStreamException and write an error message.
        catch(EndOfStreamException e)
        {
            Console.WriteLine("Error writing the data.\n{0}",
                e.GetType().Name);
        }
    }
}

備註

由於數據格式設定衝突,因此不建議搭配下列編碼方式使用此方法:

  • UTF-7

  • ISO-2022-JP

  • ISCII

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Boolean)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

寫入一位元組的 Boolean 值至目前資料流,其中 0 表示 false,1 表示 true

C#
public virtual void Write (bool value);

參數

value
Boolean

要寫入的 Boolean 值 (0 或 1)。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何在檔案中儲存和擷取應用程式設定。

C#
using System;
using System.IO;
using System.Text;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                writer.Write(1.250F);
                writer.Write(@"c:\Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

備註

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Int32)

來源:
BinaryWriter.cs
來源:
BinaryWriter.cs
來源:
BinaryWriter.cs

將四位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。

C#
public virtual void Write (int value);

參數

value
Int32

要寫入的四位元組帶正負號的整數。

例外狀況

發生 I/O 錯誤。

資料流已關閉。

範例

下列程式代碼範例示範如何在檔案中儲存和擷取應用程式設定。

C#
using System;
using System.IO;
using System.Text;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                writer.Write(1.250F);
                writer.Write(@"c:\Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

備註

BinaryWriter 以小到尾格式儲存此資料類型。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0