資訊
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BinaryWriter.Write 方法 (String)

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

命名空間:  System.IO
組件:  mscorlib (在 mscorlib.dll 中)

public virtual void Write(
	string value
)

參數

value
類型:System.String
要寫入的值。

例外狀況條件
IOException

發生 I/O 錯誤。

ArgumentNullException

valuenull

ObjectDisposedException

已關閉資料流。

Length-prefixed means that this method first writes the length of the string, in bytes, when encoded with the BinaryWriter instance's current encoding to the stream. This value is written as an UTF-7 encoded unsigned integer. This method then writes that many bytes to the stream.

For example, the string “A” has a length of 1, but when encoded with UTF-16; the length is 2 bytes, so the value written in the prefix is 2, and 3 bytes are written to the stream, including the prefix.

For a list of common I/O tasks, see 一般 I/O 工作.

The following code example demonstrates how to store and retrieve application settings in a file.


using System;
using System.IO;

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

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

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            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 (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                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);
        }
    }
}


.NET Framework

支援版本:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

支援版本:4、3.5 SP1

可攜式類別庫

支援版本:可攜式類別庫

適用於 Windows 市集應用程式的 .NET

支援版本:Windows 8

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)

此.NET Framework 並不支援各種平台的所有版本。如需支援版本的清單,請參閱.NET Framework 系統需求.

社群新增項目

顯示:
© 2014 Microsoft