Export (0) Print
Expand All

StreamWriter.AutoFlush Property

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to StreamWriter.Write.

[Visual Basic]
Public Overridable Property AutoFlush As Boolean
[C#]
public virtual bool AutoFlush {get; set;}
[C++]
public: __property virtual bool get_AutoFlush();
public: __property virtual void set_AutoFlush(bool);
[JScript]
public function get AutoFlush() : Boolean;
public function set AutoFlush(Boolean);

Property Value

true to force StreamWriter to flush its buffer; otherwise, false.

Remarks

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

When AutoFlush is set to false, StreamWriter will do a limited amount of buffering, both internally and potentially in the encoder from the encoding you passed in. You can get better performance by setting AutoFlush to false, assuming that you always call Close (or at least Flush) when you're done writing with a StreamWriter.

For example, set AutoFlush to true when you are writing to a device where the user expects immediate feedback. Console.Out is one of these cases: The StreamWriter used internally for writing to Console flushes all its internal state except the encoder state after every call to StreamWriter.Write.

For an example of using this property, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File

Example

[Visual Basic, C#, C++] The following example shows the syntax for using the AutoFlush property.

[Visual Basic] 
' Gets or sets a value indicating whether the StreamWriter
' will flush its buffer to the underlying stream after every 
' call to StreamWriter.Write.
Sw.AutoFlush = True

[C#] 
// Gets or sets a value indicating whether the StreamWriter
// will flush its buffer to the underlying stream after every 
// call to StreamWriter.Write.
sw.AutoFlush = true;                             

[C++] 
// Gets or sets a value indicating whether the StreamWriter
// will flush its buffer to the underlying stream after every 
// call to StreamWriter.Write.
sw->AutoFlush = true;                             

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

StreamWriter Class | StreamWriter Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft