Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
 WriteAllText Method (My.Computer.Fi...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Basic Language Reference
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.

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

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
append: use True to append, False to overwrite; no defaults for append, encoding      gerry lowry   |   Edit   |   Show History
This help text could be more explicit. It also contains errors.

append

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


The above next needs to be more explicit. Also, "Default is False" is invalid.

Example:
append

Boolean. True to append text; False to overwrite existing text. Required.



Likewise, regarding encoding, "Default is UTF-8" is invalid.

There are no overlodes for append or encoding. No default parameters exist.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker