File.WriteAllText 方法

定义

创建一个新文件,向其中写入内容,然后关闭文件。 如果目标文件已存在,则会将其截断并覆盖。

重载

WriteAllText(String, String)

创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则会将其截断并覆盖。

WriteAllText(String, String, Encoding)

创建一个新文件,使用指定编码向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则会将其截断并覆盖。

WriteAllText(String, String)

创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则会将其截断并覆盖。

public:
 static void WriteAllText(System::String ^ path, System::String ^ contents);
public static void WriteAllText (string path, string contents);
public static void WriteAllText (string path, string? contents);
static member WriteAllText : string * string -> unit
Public Shared Sub WriteAllText (path As String, contents As String)

参数

path
String

要写入的文件。

contents
String

要写入文件的字符串。

例外

.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

- 或 -

path 指定了一个隐藏文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 WriteAllText 方法将文本写入文件。 在此示例中,如果文件尚不存在,则会创建一个文件,并将文本添加到该文件中。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注解

此方法使用没有 Byte-Order Mark (BOM) 的 UTF-8 编码,因此使用 GetPreamble 方法将返回空字节数组。 如果需要在文件的开头包含 UTF-8 标识符(如字节顺序标记),请使用 WriteAllText(String, String, Encoding) 编码方法 UTF8 重载。

给定字符串和文件路径后,此方法将打开指定的文件,将字符串写入文件,然后关闭该文件。

适用于

WriteAllText(String, String, Encoding)

创建一个新文件,使用指定编码向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则会将其截断并覆盖。

public:
 static void WriteAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void WriteAllText (string path, string contents, System.Text.Encoding encoding);
public static void WriteAllText (string path, string? contents, System.Text.Encoding encoding);
static member WriteAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub WriteAllText (path As String, contents As String, encoding As Encoding)

参数

path
String

要写入的文件。

contents
String

要写入文件的字符串。

encoding
Encoding

应用于字符串的编码。

例外

.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

- 或 -

path 指定了一个隐藏文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 WriteAllText 方法将文本写入文件。 在此示例中,如果文件尚不存在,则会创建一个文件,并将文本添加到该文件中。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText =
        "Hello and Welcome" + Environment.NewLine

    File.WriteAllText(path, createText, Encoding.UTF8)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText As String = "Hello and Welcome" + Environment.NewLine
            File.WriteAllText(path, createText, Encoding.UTF8)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText, Encoding.UTF8)

        ' Open the file to read from.
        Dim readText As String = File.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

注解

给定字符串和文件路径后,此方法将打开指定的文件,使用指定的编码将字符串写入文件,然后关闭该文件。 即使引发异常,也保证通过此方法关闭文件句柄。

适用于