Udostępnij za pośrednictwem


My.Computer.FileSystem.OpenTextFileWriter Method

Opens a StreamWriter.

' Usage
Dim value As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(file ,append)
Dim value As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(file ,append ,encoding)
' Declaration
Public Function OpenTextFileWriter( _
   ByVal file As String, _
   ByVal append As Boolean _
) As System.IO.StreamWriter
' -or-
Public Function OpenTextFileWriter( _
   ByVal file As String, _
   ByVal append As Boolean, _
   ByVal encoding As System.Text.Encoding _
) As System.IO.StreamWriter

Parameters

  • file
    String. File to be written to. Required.

  • append
    Boolean. Specifies whether to append to or overwrite information in the file. Required.

  • encoding
    Encoding. Encoding to be used in writing to the file. Default is UTF8.

Return Value

StreamWriter

Exceptions

The following condition may cause an exception to be thrown:

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 the append parameter is True, the method appends the text to the file; otherwise existing text in the file is overwritten.

If you are writing only a few strings to a file, it might be simpler to use the WriteAllText method. For more information, see My.Computer.FileSystem.WriteAllText Method.

Tasks

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

To

See

Write text to a file by using a StreamWriter

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

Example

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

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

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permission may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

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

Reference

My.Computer.FileSystem Object

System.Text.Encoding

System.IO.StreamWriter

FileSystem.OpenTextFileWriter

My.Computer.FileSystem.WriteAllText Method