Export (0) Print
Expand All
Expand Minimize

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

Parameters

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 writing to 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 due to insufficient privileges. 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 System.Text.Encoding.Default, which uses the system's current ANSI code page.

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

If append is set to False, existing text in the file is overwritten.

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

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 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 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.

Community Additions

ADD
Show:
© 2015 Microsoft