Redigera

Dela via


FileSystem.OpenTextFileWriter Method

Definition

Opens a StreamWriter object to write to the specified file.

Overloads

OpenTextFileWriter(String, Boolean)

Opens a StreamWriter object to write to the specified file.

OpenTextFileWriter(String, Boolean, Encoding)

Opens a StreamWriter to write to the specified file.

OpenTextFileWriter(String, Boolean)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Opens a StreamWriter object to write to the specified file.

public:
 static System::IO::StreamWriter ^ OpenTextFileWriter(System::String ^ file, bool append);
public static System.IO.StreamWriter OpenTextFileWriter (string file, bool append);
static member OpenTextFileWriter : string * bool -> System.IO.StreamWriter
Public Shared Function OpenTextFileWriter (file As String, append As Boolean) As StreamWriter

Parameters

file
String

File to be written to.

append
Boolean

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

Returns

StreamWriter object to write to the specified file.

Exceptions

file is Nothing or an empty string.

The file name ends with a trailing slash.

Examples

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()

Remarks

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.

Note

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

See also

Applies to

OpenTextFileWriter(String, Boolean, Encoding)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Opens a StreamWriter to write to the specified file.

public:
 static System::IO::StreamWriter ^ OpenTextFileWriter(System::String ^ file, bool append, System::Text::Encoding ^ encoding);
public static System.IO.StreamWriter OpenTextFileWriter (string file, bool append, System.Text.Encoding encoding);
static member OpenTextFileWriter : string * bool * System.Text.Encoding -> System.IO.StreamWriter
Public Shared Function OpenTextFileWriter (file As String, append As Boolean, encoding As Encoding) As StreamWriter

Parameters

file
String

File to be written to.

append
Boolean

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

encoding
Encoding

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

Returns

StreamWriter object to write to the specified file.

Exceptions

file is Nothing or an empty string.

The file name ends with a trailing slash.

Examples

This example opens a StreamWriter with the My.Computer.FileSystem.OpenTextFileWriter method with Unicode encoding 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, Text.Encoding.Unicode)
file.WriteLine("Here is the first string.")
file.Close()

Remarks

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.

Note

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

See also

Applies to