WriteAllText Method


WriteAllText Method

Location

My.Computer.FileSystem.WriteAllText

Syntax

     My.Computer.FileSystem.WriteAllText(path, textData, _    append[, encoding]) 


path (required; String)

The path of the file to be written.


textData (required; String)

The text to be written to the file.


append (required; Boolean)

Indicates whether the text will be appended to the end of the file's existing content (true) or replaces any existing content (False).


encoding (optional; Encoding)

The character-encoding method to use as a System.Text.Encoding object. If this parameter is missing, UTF-8 is used as the default encoding method.

Description

The WriteAllText method writes the content of a string to a specified file. If the file does not exist, it will be created, as long as the path specified with the file name is valid.

Usage at a Glance

  • An exception is thrown if the path parameter is missing or invalid or if the file exists and is in use.

  • An exception is thrown if the user lacks sufficient file-access permissions.

  • If the file already exists and uses an encoding method other than the one specified by this method, the original encoding method of the file will prevail.

Example

The following example appends text to a file.

     Public Sub RecordError(ByVal errorText As String)        My.Computer.FileSystem.WriteAllText("C:\error.log", _           errorText & vbCrLf, True)     End Sub 

Related Framework Entries

  • Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText Method

  • Microsoft.VisualBasic.MyServices.FileSystemProxy.WriteAllText Method

See Also

FileSystem Object, OpenTextFieldParser Method, OpenTextFileReader Method, OpenTextFileWriter Method, ReadAllBytes Method, ReadAllText Method, WriteAllBytes Method




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net