File.WriteAllText 方法

定義

建立一個新檔案,將內容寫入檔案,然後關閉該檔案。 如果目標檔案已經存在,則會遭到截斷並覆寫。

多載

名稱 Description
WriteAllText(String, ReadOnlySpan<Char>)

建立一個新檔案,將指定的字串寫入檔案,然後關閉該檔案。

如果目標檔案已經存在,則會遭到截斷並覆寫。

WriteAllText(String, String)

建立一個新檔案,將指定的字串寫入檔案,然後關閉該檔案。 如果目標檔案已經存在,則會遭到截斷並覆寫。

WriteAllText(String, ReadOnlySpan<Char>, Encoding)

建立一個新檔案,使用指定的編碼寫入指定的字串,然後關閉該檔案。

如果目標檔案已經存在,則會遭到截斷並覆寫。

WriteAllText(String, String, Encoding)

建立一個新檔案,使用指定的編碼寫入指定的字串,然後關閉該檔案。 如果目標檔案已經存在,則會遭到截斷並覆寫。

WriteAllText(String, ReadOnlySpan<Char>)

來源:
File.cs
來源:
File.cs
來源:
File.cs

建立一個新檔案,將指定的字串寫入檔案,然後關閉該檔案。

如果目標檔案已經存在,則會遭到截斷並覆寫。

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

參數

path
String

要寫入的檔案。

contents
ReadOnlySpan<Char>

寫入檔案的字元。

例外狀況

pathnull

path 空無一人。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

指定的路徑無效(例如,它位於未對應的磁碟驅動器上)。

開啟檔案時發生 I/O 錯誤。

path 指定一個唯讀的檔案。

-或-

path 指定一個隱藏的檔案。

-或-

path 指定一個目錄。

-或-

目前平臺不支援此作業。

來電者沒有所需的權限。

path 格式無效。

備註

此方法使用 UTF-8 編碼,無 Byte-Order 標記(BOM),因此使用此 GetPreamble() 方法會回傳一個空的位元組陣列。 如果需要在檔案開頭加入 UTF-8 識別碼,例如位元組順序標記,請使用此 WriteAllText(String, ReadOnlySpan<Char>, Encoding) 方法。

適用於

WriteAllText(String, String)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs

建立一個新檔案,將指定的字串寫入檔案,然後關閉該檔案。 如果目標檔案已經存在,則會遭到截斷並覆寫。

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

備註

此方法使用 UTF-8 編碼,無 Byte-Order 標記(BOM),因此使用此 GetPreamble 方法會回傳一個空的位元組陣列。 如果檔案開頭必須包含 UTF-8 識別碼,例如位元組順序標記,請使用 WriteAllText(String, String, Encoding) 方法過載與 UTF8 編碼。

給定一個字串和一個檔案路徑,此方法會開啟指定的檔案,將字串寫入該檔案,然後關閉該檔案。

適用於

WriteAllText(String, ReadOnlySpan<Char>, Encoding)

來源:
File.cs
來源:
File.cs
來源:
File.cs

建立一個新檔案,使用指定的編碼寫入指定的字串,然後關閉該檔案。

如果目標檔案已經存在,則會遭到截斷並覆寫。

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

參數

path
String

要寫入的檔案。

contents
ReadOnlySpan<Char>

寫入檔案的字元。

encoding
Encoding

編碼要套用到字串上。

例外狀況

pathencodingnull

path 空無一人。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

指定的路徑無效(例如,它位於未對應的磁碟驅動器上)。

開啟檔案時發生 I/O 錯誤。

path 指定一個唯讀的檔案。

-或-

path 指定一個隱藏的檔案。

-或-

path 指定一個目錄。

-或-

來電者沒有所需的權限。

-或-

目前平臺不支援此作業。

path 格式無效。

適用於

WriteAllText(String, String, Encoding)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs

建立一個新檔案,使用指定的編碼寫入指定的字串,然後關閉該檔案。 如果目標檔案已經存在,則會遭到截斷並覆寫。

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

備註

給定一個字串和一個檔案路徑,此方法會打開指定的檔案,並以指定的編碼將字串寫入檔案,然後關閉該檔案。 即使提出例外,檔案柄也保證會被此方法關閉。

適用於