C# BinaryWriter類


C# BinaryWriter類用於將二進位制資訊寫入流中。它在System.IO名稱空間中找到。它還支援以特定編碼編寫字串。

C# BinaryWriter範例

我們來看看將資料寫入資料檔案的BinaryWriter類的簡單例子。

using System;  
using System.IO;  
namespace BinaryWriterExample  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string fileName = "e:\\binaryfile.dat";  
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))  
            {  
                writer.Write(1024);  
                writer.Write("this is string data");  
                writer.Write(true);  
            }  
            Console.WriteLine("Data written successfully...");    
        }  
    }  
}

執行上面範例程式碼,得到以下結果 -

Data written successfully...