Export (0) Print
Expand All

FileSystem.WriteAllText Method (String, String, Boolean, Encoding)

Writes text to a file.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void WriteAllText(
	string file,
	string text,
	bool append,
	Encoding encoding
)

Parameters

file
Type: System.String

File to be written to.

text
Type: System.String

Text to be written to file.

append
Type: System.Boolean

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

encoding
Type: System.Text.Encoding

What encoding to use when writing to file.

ExceptionCondition
ArgumentException

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\); it ends with a trailing slash.

ArgumentNullException

file is Nothing.

FileNotFoundException

The file does not exist.

IOException

The file is in use by another process, or an I/O error occurs.

PathTooLongException

The path exceeds the system-defined maximum length.

NotSupportedException

A file or directory name in the path contains a colon (:) or is in an invalid format.

OutOfMemoryException

There is not enough memory to write the string to buffer.

SecurityException

The user lacks necessary permissions to view the path.

If the specified file does not exist, it is created.

If the specified encoding does not match the existing encoding of the file, the specified coding is ignored.

NoteNote

The WriteAllText method opens a file, writes to it, and then closes it. Code that uses the WriteAllText method is simpler than code that uses a StreamWriter object. However, if you are adding strings to a file by using a loop, a StreamWriter object can provide better performance because you only have to open and close the file one time. For more information, see the OpenTextFileWriter method.

The following table lists examples of tasks involving the My.Computer.FileSystem.WriteAllText method.

This example writes the line "This is new text to be added." to the file Test.txt, overwriting any existing text in the file.

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",
"This is new text to be added.", False)

This example writes the names of the files in the Documents and Settings folder to FileList.txt, inserting a carriage return between each for better readability.

For Each foundFile In
        My.Computer.FileSystem.GetFiles("C:\Documents and Settings")
    foundFile = foundFile & vbCrLf
    My.Computer.FileSystem.WriteAllText(
        "C:\Documents and Settings\FileList.txt", foundFile, True)
Next

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft