FileSystemProxy.WriteAllText Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Writes text to a file.
WriteAllText(String, String, Boolean) |
Writes text to a file. |
WriteAllText(String, String, Boolean, Encoding) |
Writes text to a file. |
- Source:
- FileSystemProxy.vb
- Source:
- FileSystemProxy.vb
- Source:
- FileSystemProxy.vb
Writes text to a file.
public:
void WriteAllText(System::String ^ file, System::String ^ text, bool append);
public void WriteAllText (string file, string text, bool append);
member this.WriteAllText : string * string * bool -> unit
Public Sub WriteAllText (file As String, text As String, append As Boolean)
Parameters
- file
- String
File to be written to.
- text
- String
Text to be written to file.
- append
- Boolean
True
to append to the contents of the file; False
to overwrite the contents of the file. Default is False
.
Exceptions
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; or it is a device path (starts with \.\); it ends with a trailing slash.
file
is Nothing
.
The file does not exist.
The file is in use by another process, or an I/O error occurs.
The path exceeds the system-defined maximum length.
A file or directory name in the path contains a colon (:) or is in an invalid format.
There is not enough memory to write the string to buffer.
The user lacks necessary permissions to view the path.
Examples
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 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
Remarks
The UTF-8 encoding is used to write to the file. To specify a different encoding, use a different overload of the WriteAllText method.
If the specified file does not exist, it is created.
If the specified encoding does not match the existing encoding of the file, the specified coding is ignored.
Athugasemd
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 the OpenTextFileWriter method.
The following table lists examples of tasks involving the My.Computer.FileSystem.WriteAllText
method.
To | See |
---|---|
Write text to a file | How to: Write Text to Files |
Append text to a file | How to: Append to Text Files |
See also
- Encoding
- WriteAllText(String, String, Boolean)
- Objects (Visual Basic)
- Writing to Files in Visual Basic
Applies to
.NET Framework 4.8.1 og aðrar útgáfur
Vara | Útgáfur |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 5, 6, 7, 8, 9 |
- Source:
- FileSystemProxy.vb
- Source:
- FileSystemProxy.vb
- Source:
- FileSystemProxy.vb
Writes text to a file.
public:
void WriteAllText(System::String ^ file, System::String ^ text, bool append, System::Text::Encoding ^ encoding);
public void WriteAllText (string file, string text, bool append, System.Text.Encoding encoding);
member this.WriteAllText : string * string * bool * System.Text.Encoding -> unit
Public Sub WriteAllText (file As String, text As String, append As Boolean, encoding As Encoding)
Parameters
- file
- String
File to be written to.
- text
- String
Text to be written to file.
- append
- Boolean
True
to append to the contents of the file; False
to overwrite the contents of the file. Default is False
.
- encoding
- Encoding
What encoding to use when writing to file. Default is UTF-8.
Exceptions
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; or it is a device path (starts with \.\); it ends with a trailing slash.
file
is Nothing
.
The file does not exist.
The file is in use by another process, or an I/O error occurs.
The path exceeds the system-defined maximum length.
A file or directory name in the path contains a colon (:) or is in an invalid format.
There is not enough memory to write the string to buffer.
The user lacks necessary permissions to view the path.
Examples
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 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
Remarks
If the specified file does not exist, it is created.
If the specified encoding does not match the existing encoding of the file, the specified coding is ignored.
Athugasemd
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 the OpenTextFileWriter method.
The following table lists examples of tasks involving the My.Computer.FileSystem.WriteAllText
method.
To | See |
---|---|
Write text to a file | How to: Write Text to Files |
Append text to a file | How to: Append to Text Files |
See also
Applies to
.NET Framework 4.8.1 og aðrar útgáfur
Vara | Útgáfur |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 5, 6, 7, 8, 9 |
.NET umsögn
.NET er opið verkefni. Veldu tengil til að gefa umsögn: