Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Basic
Objects
 WriteAllText Method (My.Computer.Fi...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

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

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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
My.Computer.FileSystem.WriteAllText(String, String, Boolean) uses UTF-8 as the default encoding      HuyN_MS ... treesawat   |   Edit   |   Show History

As mentioned in the documentation, the default encoding is UTF-8. If you use My.Computer.FileSystem.WriteAllText(String, String, Boolean) to a file, this file will have additional 3 bytes at the beginning. These 3 bytes are the Byte-Order-Mark (BOM) of UTF-8 encoding.

To quickly write text to a file without the BOM, you can specify the ASCII encoding with My.Computer.FileSystem.WriteAllText(String, String, Boolean, Encoding) method.

For example,

        My.Computer.FileSystem.WriteAllText( _
"Test.txt", "ASCII text", False, System.Text.Encoding.ASCII)
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker