File.WriteAllText File.WriteAllText File.WriteAllText File.WriteAllText Method

定義

建立新檔案,將內容寫入檔案,然後關閉檔案。Creates a new file, write the contents to the file, and then closes the file. 如果檔案已經存在,則會覆寫該檔案。If the target file already exists, it is overwritten.

多載

WriteAllText(String, String) WriteAllText(String, String) WriteAllText(String, String) WriteAllText(String, String)

建立新檔案,將指定的字串寫入檔案,然後關閉檔案。Creates a new file, writes the specified string to the file, and then closes the file. 如果檔案已經存在,則會覆寫該檔案。If the target file already exists, it is overwritten.

WriteAllText(String, String, Encoding) WriteAllText(String, String, Encoding) WriteAllText(String, String, Encoding)

建立新檔案,使用指定的編碼方式將指定的字串寫入檔案,然後關閉檔案。Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. 如果檔案已經存在,則會覆寫該檔案。If the target file already exists, it is overwritten.

WriteAllText(String, String) WriteAllText(String, String) WriteAllText(String, String) WriteAllText(String, String)

建立新檔案,將指定的字串寫入檔案,然後關閉檔案。Creates a new file, writes the specified string to the file, and then closes the file. 如果檔案已經存在,則會覆寫該檔案。If the target file already exists, it is overwritten.

public:
 static void WriteAllText(System::String ^ path, System::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 String String String

要寫入其中的檔案。The file to write to.

contents
String String String String

要寫入檔案的字串。The string to write to the file.

例外狀況

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

開啟檔案時發生 I/O 錯誤。An I/O error occurred while opening the file.

path 指定了唯讀的檔案。path specified a file that is read-only.

-或--or- path 指定了隱藏的檔案。path specified a file that is hidden.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

-或--or- path 指定了目錄。path specified a directory.

-或--or- 呼叫端沒有必要的權限。The caller does not have the required permission.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列程式碼範例示範如何使用WriteAllText方法,將文字寫入檔案。The following code example demonstrates the use of the WriteAllText method to write text to a file. 在此範例中,會建立檔案(如果尚未存在),並將文字加入其中。In this example a file is created, if it doesn't already exist, and text is added to it.

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);
    }
}
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

備註

這個方法會使用不含位元組順序標記(BOM)的 utf-8 編碼,因此使用GetPreamble方法將會傳回空的位元組陣列。This method uses UTF-8 encoding without a Byte-Order Mark (BOM), so using the GetPreamble method will return an empty byte array. 如果必須在檔案開頭包含 utf-8 識別碼(例如位元組順序標記),請使用WriteAllText(String, String, Encoding)方法UTF8多載搭配編碼。If it is necessary to include a UTF-8 identifier, such as a byte order mark, at the beginning of a file, use the WriteAllText(String, String, Encoding) method overload with UTF8 encoding.

指定字串和檔案路徑時,這個方法會開啟指定的檔案,將字串寫入檔案,然後關閉檔案。Given a string and a file path, this method opens the specified file, writes the string to the file, and then closes the file.

安全性

FileIOPermission
適用于寫入檔案或目錄的存取權。for access to write to a file or directory. 相關聯的列舉:WriteAssociated enumeration: Write

WriteAllText(String, String, Encoding) WriteAllText(String, String, Encoding) WriteAllText(String, String, Encoding)

建立新檔案,使用指定的編碼方式將指定的字串寫入檔案,然後關閉檔案。Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. 如果檔案已經存在,則會覆寫該檔案。If the target file already exists, it is overwritten.

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);
static member WriteAllText : string * string * System.Text.Encoding -> unit

參數

path
String String String String

要寫入其中的檔案。The file to write to.

contents
String String String String

要寫入檔案的字串。The string to write to the file.

encoding
Encoding Encoding Encoding Encoding

要套用至字串的編碼方式。The encoding to apply to the string.

例外狀況

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

開啟檔案時發生 I/O 錯誤。An I/O error occurred while opening the file.

path 指定了唯讀的檔案。path specified a file that is read-only.

-或--or- path 指定了隱藏的檔案。path specified a file that is hidden.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

-或--or- path 指定了目錄。path specified a directory.

-或--or- 呼叫端沒有必要的權限。The caller does not have the required permission.

呼叫端沒有必要的權限。The caller does not have the required permission.

範例

下列程式碼範例示範如何使用WriteAllText方法,將文字寫入檔案。The following code example demonstrates the use of the WriteAllText method to write text to a file. 在此範例中,會建立檔案(如果尚未存在),並將文字加入其中。In this example a file is created, if it doesn't already exist, and text is added to it.

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);
    }
}
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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案、使用指定的編碼方式將字串寫入檔案,然後關閉檔案。Given a string and a file path, this method opens the specified file, writes the string to the file using the specified encoding, and then closes the file. 即使引發例外狀況,也一定會由這個方法關閉檔案控制代碼。The file handle is guaranteed to be closed by this method, even if exceptions are raised.

安全性

FileIOPermission
適用于寫入檔案或目錄的存取權。for access to write to a file or directory. 相關聯的列舉:WriteAssociated enumeration: Write

適用於