Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FileSystem.OpenTextFileWriter Method (String, Boolean, Encoding)

Opens a StreamWriter to write to the specified file.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static StreamWriter OpenTextFileWriter(
	string file,
	bool append,
	Encoding encoding
)

Parameters

file
Type: System.String

File to be written to.

append
Type: System.Boolean

True to append to the contents in the file; False to overwrite the contents of the file. Default is False.

encoding
Type: System.Text.Encoding

Encoding to be used in writing to the file. Default is ASCII.

Return Value

Type: System.IO.StreamWriter
StreamWriter object to write to the specified file.
ExceptionCondition
ArgumentException

The file name ends with a trailing slash.

The OpenTextFileWriter method opens and initializes a stream for a file and then returns the StreamWriter object for that stream. You can write to the steam as many times as necessary and then close it when you are finished.

NoteNote

You must call the Close method on the StreamWriter object to make sure that all data is correctly written to the underlying stream.

If you are writing only a few strings to a file, it might be simpler to use the WriteAllText method.

The following table lists an example of a task involving the My.Computer.FileSystem.OpenTextFileWriter method.

To

See

Write text to a file with a StreamWriter

How to: Write Text to Files with a StreamWriter in Visual Basic

This example opens a StreamWriter with the My.Computer.FileSystem.OpenTextFileWriter method and uses it to write a string to a text file with the WriteLine method of the StreamWriter class.

Dim file = My.Computer.FileSystem.OpenTextFileWriter(
    "c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.