File.WriteAllText 方法

定義

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

多載

WriteAllText(String, String)

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

WriteAllText(String, String, Encoding)

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

WriteAllText(String, String)

來源:
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 和 2.1 之前的 .NET Core 版本: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 Mark (BOM) ,因此使用 GetPreamble 方法會傳回空的位元組數位。 如果需要在檔案開頭包含UTF-8標識元,例如位元組順序標記,請使用 WriteAllText(String, String, Encoding) 方法多載搭配 UTF8 編碼。

指定字串和檔案路徑時,這個方法會開啟指定的檔案、將字串寫入檔案,然後關閉檔案。

適用於

WriteAllText(String, String, Encoding)

來源:
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 和 2.1 之前的 .NET Core 版本: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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案、使用指定的編碼將字串寫入檔案,然後關閉檔案。 即使引發例外狀況,這個方法仍保證會關閉檔句柄。

適用於