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.
Exceptions
The following conditions may cause an exception:
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, ends with a trailing slash, or is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
file points to a path that does not exist (FileNotFoundException or DirectoryNotFoundException).
The file is in use by another process, or an I/O error occurs (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
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.
Remarks
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.
Tasks
The following table lists examples of tasks involving the My.Computer.FileSystem.WriteAllText method.
To | See |
---|---|
Write text to a file |
|
Append text to a file |
Example
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
Requirements
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 |
Permissions
The following permission may be necessary:
Permission | Description |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Reference
My.Computer.FileSystem Object
System.Text.Encoding
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText