FileSystem.WriteAllText 方法

定义

将文本写入文件。

重载

名称 说明
WriteAllText(String, String, Boolean)

将文本写入文件。

WriteAllText(String, String, Boolean, Encoding)

将文本写入文件。

WriteAllText(String, String, Boolean)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

将文本写入文件。

public:
 static void WriteAllText(System::String ^ file, System::String ^ text, bool append);
public static void WriteAllText(string file, string text, bool append);
static member WriteAllText : string * string * bool -> unit
Public Shared Sub WriteAllText (file As String, text As String, append As Boolean)

参数

file
String

要写入的文件。

text
String

要写入文件的文本。

append
Boolean

若要追加到文件的内容,则为 ; 如果覆盖文件的内容,则为

例外

路径对于以下原因之一无效:它是零长度字符串;它仅包含空格;它包含无效字符;或者它是设备路径(以 \\.\; 开头);它以尾部斜杠结尾。

fileNothing

该文件不存在。

该文件由另一个进程使用,或发生 I/O 错误。

路径超过系统定义的最大长度。

路径中的文件或目录名称包含冒号(:)或格式无效)。

没有足够的内存将字符串写入缓冲区。

用户缺少查看路径所需的权限。

示例

本示例将行 "This is new text to be added." 写入文件 Test.txt,覆盖文件中的任何现有文本。

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",
"This is new text to be added.", False)

本示例将文件夹中文件的名称写入其中Documents and Settings,并在每个文件夹FileList.txt之间插入回车,以提高可读性。

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

注解

UTF-8 编码用于写入文件。 若要指定不同的编码,请使用方法的不同重载 WriteAllText

如果指定的文件不存在,则会创建该文件。

如果指定的编码与文件的现有编码不匹配,则忽略指定的编码。

注释

该方法 WriteAllText 打开一个文件,写入该文件,然后将其关闭。 使用 WriteAllText 该方法的代码比使用 StreamWriter 对象的代码更简单。 但是,如果要使用循环向文件添加字符串,则对象可以提供更好的性能, StreamWriter 因为只需打开并关闭一次文件。 有关更多信息,请参见 OpenTextFileWriter 方法。

下表列出了涉及 My.Computer.FileSystem.WriteAllText 该方法的任务的示例。

收件人 请参阅
将文本写入文件 如何:在 Visual Basic
将文本追加到文件 如何:在 Visual Basic

另请参阅

适用于

WriteAllText(String, String, Boolean, Encoding)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

将文本写入文件。

public:
 static void WriteAllText(System::String ^ file, System::String ^ text, bool append, System::Text::Encoding ^ encoding);
public static void WriteAllText(string file, string text, bool append, System.Text.Encoding encoding);
static member WriteAllText : string * string * bool * System.Text.Encoding -> unit
Public Shared Sub WriteAllText (file As String, text As String, append As Boolean, encoding As Encoding)

参数

file
String

要写入的文件。

text
String

要写入文件的文本。

append
Boolean

若要追加到文件的内容,则为 ; 如果覆盖文件的内容,则为

encoding
Encoding

写入文件时要使用的编码。

例外

路径对于以下原因之一无效:它是零长度字符串;它仅包含空格;它包含无效字符;或者它是设备路径(以 \\.\; 开头);它以尾部斜杠结尾。

fileNothing

该文件不存在。

该文件由另一个进程使用,或发生 I/O 错误。

路径超过系统定义的最大长度。

路径中的文件或目录名称包含冒号(:)或格式无效)。

没有足够的内存将字符串写入缓冲区。

用户缺少查看路径所需的权限。

示例

本示例将行 "This is new text to be added." 写入文件 Test.txt,覆盖文件中的任何现有文本。

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",
"This is new text to be added.", False)

本示例将文件夹中文件的名称写入其中Documents and Settings,并在每个文件夹FileList.txt之间插入回车,以提高可读性。

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

注解

如果指定的文件不存在,则会创建该文件。

如果指定的编码与文件的现有编码不匹配,则忽略指定的编码。

注释

该方法 WriteAllText 打开一个文件,写入该文件,然后将其关闭。 使用 WriteAllText 该方法的代码比使用 StreamWriter 对象的代码更简单。 但是,如果要使用循环向文件添加字符串,则对象可以提供更好的性能, StreamWriter 因为只需打开并关闭一次文件。 有关更多信息,请参见 OpenTextFileWriter 方法。

下表列出了涉及 My.Computer.FileSystem.WriteAllText 该方法的任务的示例。

收件人 请参阅
将文本写入文件 如何:在 Visual Basic
将文本追加到文件 如何:在 Visual Basic

另请参阅

适用于