This documentation is archived and is not being maintained.

My.Computer.FileSystem.WriteAllText Method

Writes text to a file.

' Usage
My.Computer.FileSystem.WriteAllText(file ,text ,append)
My.Computer.FileSystem.WriteAllText(file ,text ,append ,encoding)
' Declaration
Public Sub WriteAllText( _
   ByVal file As String, _
   ByVal text As String, _
   ByVal append As Boolean _
)
' -or-
Public Sub WriteAllText( _
   ByVal file As String, _
   ByVal text As String, _
   ByVal append As Boolean, _
   ByVal encoding As System.Text.Encoding _
)

file

String. File to be written to. Required.

text

String. Text to be written to file. Required.

append

Boolean. Whether to append text or overwrite existing text. Default is False. Required.

encoding

Encoding. What encoding to use when you write to the file. Required. Default is UTF-8.

The following conditions may cause an exception:

If you are running in a partial-trust context, the code might throw an exception because of insufficient permissions. For more information, see Code Access Security Basics.

When no encoding is specified, UTF-8 is used. The byte order mark (BOM) for the encoding is written to the file unless you specify Encoding.Default, which uses the system's current ANSI code page. If the specified encoding does not match the existing encoding of the file, the specified encoding is ignored.

If the specified path, excluding the file name, is not valid, a DirectoryNotFoundException exception is thrown. If the path is valid but the file does not exist, the file is created.

If the append parameter is True, the method appends the text to the file; otherwise existing text in the file is overwritten.

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 My.Computer.FileSystem.OpenTextFileWriter Method.

The following table lists examples of tasks that involve 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 As String 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

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

The following permission may be required:

Permission

Description

FileIOPermission

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

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

Show: