File File File File Class

定義

提供建立、複製、刪除、移動和開啟單一檔案的靜態方法,並協助 FileStream 物件的建立。Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

public ref class File abstract sealed
[System.Runtime.InteropServices.ComVisible(true)]
public static class File
type File = class
Public Class File
繼承
FileFileFileFile
屬性

範例

下列範例示範如何使用File類別,以檢查是否存在的檔案,並根據結果,請建立新的檔案,或開啟現有的檔案和讀寫從它。The following example demonstrates how to use the File class to check whether a file exists, and depending on the result, either create a new file and write to it, or open the existing file and read from it. 執行程式碼之前, 建立c:\temp資料夾。Before running the code, create a c:\temp folder.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   if (  !File::Exists( path ) )
   {
      
      // Create a file to write to.
      StreamWriter^ sw = File::CreateText( path );
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
                  delete (IDisposable^)(sw);
      }
   }

   // Open the file to read from.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
            delete (IDisposable^)(sr);
   }

   try
   {
      String^ path2 = String::Concat( path, "temp" );
      
      // Ensure that the target does not exist.
      File::Delete( path2 );
      
      // Copy the file.
      File::Copy( path, path2 );
      Console::WriteLine( "{0} was copied to {1}.", path, path2 );
      
      // Delete the newly created file.
      File::Delete( path2 );
      Console::WriteLine( "{0} was successfully deleted.", path2 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path))
        {
            string s;
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        If File.Exists(path) = False Then
            ' Create a file to write to.
            Using sw As StreamWriter = File.CreateText(path)
                sw.WriteLine("Hello")
                sw.WriteLine("And")
                sw.WriteLine("Welcome")
                sw.Flush()
           End Using
        End If

        ' Open the file to read from.
        Using sr As StreamReader = File.OpenText(path)
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
        End Using
    End Sub
End Class

備註

使用File類別,例如複製、 移動、 重新命名、 建立、 開啟、 刪除和一次附加到單一檔案的一般作業。Use the File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to a single file at a time. 您也可以使用File類別來取得和設定檔屬性或DateTime資訊與建立、 存取和寫入的檔案。You can also use the File class to get and set file attributes or DateTime information related to the creation, access, and writing of a file. 如果您想要在多個檔案上執行作業,請參閱Directory.GetFilesDirectoryInfo.GetFilesIf you want to perform operations on multiple files, see Directory.GetFiles or DirectoryInfo.GetFiles.

許多File方法會傳回其他 I/O 類型,當您建立或開啟檔案。Many of the File methods return other I/O types when you create or open files. 您可以使用這些其他型別來進一步操作檔案。You can use these other types to further manipulate a file. 如需詳細資訊,請參閱特定File這類成員OpenTextCreateText,或CreateFor more information, see specific File members such as OpenText, CreateText, or Create.

因為所有File方法是靜態的則可能使用更有效率File方法,而不是對應FileInfo執行個體方法,如果您想要執行只能有一個動作。Because all File methods are static, it might be more efficient to use a File method rather than a corresponding FileInfo instance method if you want to perform only one action. 所有File方法需要您操作檔案的路徑。All File methods require the path to the file that you are manipulating.

靜態方法File類別執行的所有方法的安全性檢查。The static methods of the File class perform security checks on all methods. 如果您要重複使用的物件數次,請考慮使用對應的執行個體方法的FileInfo相反地,因為安全性檢查會不一定是必要。If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.

根據預設,新檔案的完整讀取/寫入存取權會授與所有使用者。By default, full read/write access to new files is granted to all users.

下表描述可用來自訂的各種行為的列舉File方法。The following table describes the enumerations that are used to customize the behavior of various File methods.

列舉Enumeration 描述Description
FileAccess 指定讀取和寫入檔案的存取權。Specifies read and write access to a file.
FileShare 指定已在使用的檔案所允許的存取層級。Specifies the level of access permitted for a file that is already in use.
FileMode 指定是否保留或覆寫現有檔案的內容和建立的現有檔案的要求是否會造成例外狀況。Specifies whether the contents of an existing file are preserved or overwritten, and whether requests to create an existing file cause an exception.

注意

接受的路徑做為輸入字串的成員,該路徑必須是語式正確,或引發例外狀況。In members that accept a path as an input string, that path must be well-formed or an exception is raised. 比方說,如果路徑是完整名稱,但以空格開頭,路徑不被修剪中類別的方法。For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. 因此,路徑的格式不正確,而且會引發例外狀況。Therefore, the path is malformed and an exception is raised. 同樣地,路徑的組合無法完整限定兩次。Similarly, a path or a combination of paths cannot be fully qualified twice. 例如,"c:\temp c:\windows,"也會引發例外狀況在大部分情況下。For example, "c:\temp c:\windows" also raises an exception in most cases. 請確定您的路徑是語式正確的當您使用接受路徑字串的方法。Ensure that your paths are well-formed when using methods that accept a path string.

在接受路徑的成員,路徑可以參考到檔案或只是一個目錄。In members that accept a path, the path can refer to a file or just a directory. 指定的路徑也可以參考相對路徑或通用命名慣例 (UNC) 路徑的伺服器和共用的名稱。The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. 例如,如果以下所有狀況可接受的路徑:For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt 」 以 C# 或 Visual Basic 中的 「 c:\MyDir\MyFile.txt"。"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir 」 以 C# 或 Visual Basic 中的 「 c:\mydir"。"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir 」 以 C# 或 Visual Basic 中的 「 MyDir\MySubDir"。"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • 「\\\\MyServer\\MyShare 」 在 C# 中,或 「\\MyServer\MyShare"在 Visual Basic 中。"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

方法

AppendAllLines(String, IEnumerable<String>) AppendAllLines(String, IEnumerable<String>) AppendAllLines(String, IEnumerable<String>) AppendAllLines(String, IEnumerable<String>)

在檔案中加入幾行內容,然後關閉檔案。Appends lines to a file, and then closes the file. 如果指定的檔案不存在,則這個方法會建立檔案,將指定的程式行寫入檔案,然後關閉檔案。If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

AppendAllLines(String, IEnumerable<String>, Encoding) AppendAllLines(String, IEnumerable<String>, Encoding) AppendAllLines(String, IEnumerable<String>, Encoding) AppendAllLines(String, IEnumerable<String>, Encoding)

使用指定編碼將指定的幾行內容加入檔案,然後關閉檔案。Appends lines to a file by using a specified encoding, and then closes the file. 如果指定的檔案不存在,則這個方法會建立檔案,將指定的程式行寫入檔案,然後關閉檔案。If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

AppendAllLinesAsync(String, IEnumerable<String>, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, CancellationToken)
AppendAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) AppendAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken)
AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String)

開啟檔案,將指定的字串附加至檔案,然後關閉檔案。Opens a file, appends the specified string to the file, and then closes the file. 如果檔案不存在,則這個方法會建立檔案,將指定的字串寫入檔案,然後關閉檔案。If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding)

將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。Appends the specified string to the file, creating the file if it does not already exist.

AppendAllTextAsync(String, String, CancellationToken) AppendAllTextAsync(String, String, CancellationToken) AppendAllTextAsync(String, String, CancellationToken) AppendAllTextAsync(String, String, CancellationToken)
AppendAllTextAsync(String, String, Encoding, CancellationToken) AppendAllTextAsync(String, String, Encoding, CancellationToken) AppendAllTextAsync(String, String, Encoding, CancellationToken) AppendAllTextAsync(String, String, Encoding, CancellationToken)
AppendText(String) AppendText(String) AppendText(String) AppendText(String)

建立會將 UTF-8 編碼的文字附加至現有檔案或新檔案 (如果指定的檔案不存在) 的 StreamWriterCreates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

Copy(String, String) Copy(String, String) Copy(String, String) Copy(String, String)

複製現有的檔案到新的檔案。Copies an existing file to a new file. 不允許覆寫相同名稱的檔案。Overwriting a file of the same name is not allowed.

Copy(String, String, Boolean) Copy(String, String, Boolean) Copy(String, String, Boolean) Copy(String, String, Boolean)

複製現有的檔案到新的檔案。Copies an existing file to a new file. 允許覆寫相同名稱的檔案。Overwriting a file of the same name is allowed.

Create(String) Create(String) Create(String) Create(String)

在指定的路徑中建立或覆寫檔案。Creates or overwrites a file in the specified path.

Create(String, Int32) Create(String, Int32) Create(String, Int32) Create(String, Int32)

建立或覆寫指定檔案。Creates or overwrites the specified file.

Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) Create(String, Int32, FileOptions)

建立或覆寫指定檔案,並指定緩衝區大小以及描述如何建立或覆寫檔案的 FileOptions 值。Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file.

Create(String, Int32, FileOptions, FileSecurity) Create(String, Int32, FileOptions, FileSecurity) Create(String, Int32, FileOptions, FileSecurity) Create(String, Int32, FileOptions, FileSecurity)

建立或覆寫具有指定緩衝區大小、檔案選項和檔案安全性的指定檔案。Creates or overwrites the specified file with the specified buffer size, file options, and file security.

CreateText(String) CreateText(String) CreateText(String) CreateText(String)

建立或開啟用以寫入 UTF-8 編碼文字的檔案。Creates or opens a file for writing UTF-8 encoded text. 如果檔案已經存在,將覆寫其內容。If the file already exists, its contents are overwritten.

Decrypt(String) Decrypt(String) Decrypt(String) Decrypt(String)

解密目前帳戶使用 Encrypt(String) 方法加密的檔案。Decrypts a file that was encrypted by the current account using the Encrypt(String) method.

Delete(String) Delete(String) Delete(String) Delete(String)

刪除指定的檔案。Deletes the specified file.

Encrypt(String) Encrypt(String) Encrypt(String) Encrypt(String)

加密檔案,只有使用加密檔案的帳戶才能將其解密。Encrypts a file so that only the account used to encrypt the file can decrypt it.

Exists(String) Exists(String) Exists(String) Exists(String)

判斷指定的檔案是否存在。Determines whether the specified file exists.

GetAccessControl(String) GetAccessControl(String) GetAccessControl(String) GetAccessControl(String)

取得 FileSecurity 物件,該物件封裝指定檔案的存取控制清單 (ACL) 項目。Gets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.

GetAccessControl(String, AccessControlSections) GetAccessControl(String, AccessControlSections) GetAccessControl(String, AccessControlSections) GetAccessControl(String, AccessControlSections)

取得 FileSecurity 物件,該物件會封裝特定檔案之指定類型的存取控制清單 (ACL) 項目。Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for a particular file.

GetAttributes(String) GetAttributes(String) GetAttributes(String) GetAttributes(String)

取得路徑上檔案的 FileAttributesGets the FileAttributes of the file on the path.

GetCreationTime(String) GetCreationTime(String) GetCreationTime(String) GetCreationTime(String)

傳回指定檔案或目錄的建立日期和時間。Returns the creation date and time of the specified file or directory.

GetCreationTimeUtc(String) GetCreationTimeUtc(String) GetCreationTimeUtc(String) GetCreationTimeUtc(String)

傳回指定檔案或目錄的建立日期和時間,格式為國際標準時間 (UTC)。Returns the creation date and time, in coordinated universal time (UTC), of the specified file or directory.

GetLastAccessTime(String) GetLastAccessTime(String) GetLastAccessTime(String) GetLastAccessTime(String)

傳回指定檔案或目錄上次被存取的日期和時間。Returns the date and time the specified file or directory was last accessed.

GetLastAccessTimeUtc(String) GetLastAccessTimeUtc(String) GetLastAccessTimeUtc(String) GetLastAccessTimeUtc(String)

傳回指定檔案或目錄上次被存取的日期和時間,格式為國際標準時間 (UTC)。Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.

GetLastWriteTime(String) GetLastWriteTime(String) GetLastWriteTime(String) GetLastWriteTime(String)

傳回指定檔案或目錄上次被寫入的日期和時間。Returns the date and time the specified file or directory was last written to.

GetLastWriteTimeUtc(String) GetLastWriteTimeUtc(String) GetLastWriteTimeUtc(String) GetLastWriteTimeUtc(String)

傳回指定檔案或目錄上次被寫入的日期和時間,格式為國際標準時間 (UTC)。Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.

Move(String, String) Move(String, String) Move(String, String) Move(String, String)

移動指定的檔案至新的位置,提供指定新檔名的選項。Moves a specified file to a new location, providing the option to specify a new file name.

Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean)
Open(String, FileMode) Open(String, FileMode) Open(String, FileMode) Open(String, FileMode)

在指定路徑上開啟具有讀取/寫入權限且不共用的 FileStreamOpens a FileStream on the specified path with read/write access with no sharing.

Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess) Open(String, FileMode, FileAccess)

在指定路徑上開啟具有指定模式和存取權且不共用的 FileStreamOpens a FileStream on the specified path, with the specified mode and access with no sharing.

Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare) Open(String, FileMode, FileAccess, FileShare)

在指定路徑上開啟 FileStream,假定它具有讀取、寫入或讀取/寫入存取的指定模式和指定的共用選項。Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

OpenRead(String) OpenRead(String) OpenRead(String) OpenRead(String)

開啟現有檔案來讀取。Opens an existing file for reading.

OpenText(String) OpenText(String) OpenText(String) OpenText(String)

開啟現有 UTF-8 編碼的文字檔來讀取。Opens an existing UTF-8 encoded text file for reading.

OpenWrite(String) OpenWrite(String) OpenWrite(String) OpenWrite(String)

開啟現有檔案或建立新檔案,以進行寫入。Opens an existing file or creates a new file for writing.

ReadAllBytes(String) ReadAllBytes(String) ReadAllBytes(String) ReadAllBytes(String)

開啟二進位檔案,將檔案內容讀入位元組陣列,然後關閉檔案。Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

ReadAllBytesAsync(String, CancellationToken) ReadAllBytesAsync(String, CancellationToken) ReadAllBytesAsync(String, CancellationToken) ReadAllBytesAsync(String, CancellationToken)
ReadAllLines(String) ReadAllLines(String) ReadAllLines(String) ReadAllLines(String)

開啟文字檔,讀取檔案的所有行,然後關閉檔案。Opens a text file, reads all lines of the file, and then closes the file.

ReadAllLines(String, Encoding) ReadAllLines(String, Encoding) ReadAllLines(String, Encoding) ReadAllLines(String, Encoding)

開啟檔案,以指定的編碼方式讀取檔案的所有行,然後關閉檔案。Opens a file, reads all lines of the file with the specified encoding, and then closes the file.

ReadAllLinesAsync(String, CancellationToken) ReadAllLinesAsync(String, CancellationToken) ReadAllLinesAsync(String, CancellationToken) ReadAllLinesAsync(String, CancellationToken)
ReadAllLinesAsync(String, Encoding, CancellationToken) ReadAllLinesAsync(String, Encoding, CancellationToken) ReadAllLinesAsync(String, Encoding, CancellationToken) ReadAllLinesAsync(String, Encoding, CancellationToken)
ReadAllText(String, Encoding) ReadAllText(String, Encoding) ReadAllText(String, Encoding) ReadAllText(String, Encoding)

開啟檔案,利用指定的編碼方式讀取檔案中的所有文字,然後關閉該檔案。Opens a file, reads all text in the file with the specified encoding, and then closes the file.

ReadAllText(String) ReadAllText(String) ReadAllText(String) ReadAllText(String)

開啟文字檔,讀取檔案中的所有文字,然後關閉該檔案。Opens a text file, reads all the text in the file, and then closes the file.

ReadAllTextAsync(String, CancellationToken) ReadAllTextAsync(String, CancellationToken) ReadAllTextAsync(String, CancellationToken) ReadAllTextAsync(String, CancellationToken)
ReadAllTextAsync(String, Encoding, CancellationToken) ReadAllTextAsync(String, Encoding, CancellationToken) ReadAllTextAsync(String, Encoding, CancellationToken) ReadAllTextAsync(String, Encoding, CancellationToken)
ReadLines(String) ReadLines(String) ReadLines(String) ReadLines(String)

讀取檔案的所有行。Reads the lines of a file.

ReadLines(String, Encoding) ReadLines(String, Encoding) ReadLines(String, Encoding) ReadLines(String, Encoding)

讀取具有指定編碼方式的檔案所有行。Read the lines of a file that has a specified encoding.

Replace(String, String, String) Replace(String, String, String) Replace(String, String, String) Replace(String, String, String)

用另一個檔案的內容取代指定檔案的內容,刪除原始檔案並建立已取代檔案的備份。Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file.

Replace(String, String, String, Boolean) Replace(String, String, String, Boolean) Replace(String, String, String, Boolean) Replace(String, String, String, Boolean)

用另一個檔案的內容取代指定檔案的內容,刪除原始檔案,建立已取代檔案的備份,並選擇性地忽略合併錯誤。Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors.

SetAccessControl(String, FileSecurity) SetAccessControl(String, FileSecurity) SetAccessControl(String, FileSecurity) SetAccessControl(String, FileSecurity)

FileSecurity 物件描述的存取控制清單 (ACL) 項目套用至指定的檔案。Applies access control list (ACL) entries described by a FileSecurity object to the specified file.

SetAttributes(String, FileAttributes) SetAttributes(String, FileAttributes) SetAttributes(String, FileAttributes) SetAttributes(String, FileAttributes)

在指定路徑上設定檔案的指定 FileAttributesSets the specified FileAttributes of the file on the specified path.

SetCreationTime(String, DateTime) SetCreationTime(String, DateTime) SetCreationTime(String, DateTime) SetCreationTime(String, DateTime)

設定檔案建立的日期和時間。Sets the date and time the file was created.

SetCreationTimeUtc(String, DateTime) SetCreationTimeUtc(String, DateTime) SetCreationTimeUtc(String, DateTime) SetCreationTimeUtc(String, DateTime)

設定檔案建立的日期和時間,格式為國際標準時間 (UTC)。Sets the date and time, in coordinated universal time (UTC), that the file was created.

SetLastAccessTime(String, DateTime) SetLastAccessTime(String, DateTime) SetLastAccessTime(String, DateTime) SetLastAccessTime(String, DateTime)

取得指定檔案上一次被存取的日期和時間。Sets the date and time the specified file was last accessed.

SetLastAccessTimeUtc(String, DateTime) SetLastAccessTimeUtc(String, DateTime) SetLastAccessTimeUtc(String, DateTime) SetLastAccessTimeUtc(String, DateTime)

設定指定檔案上次被存取的日期和時間,格式為國際標準時間 (UTC)。Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.

SetLastWriteTime(String, DateTime) SetLastWriteTime(String, DateTime) SetLastWriteTime(String, DateTime) SetLastWriteTime(String, DateTime)

設定指定檔案上次被寫入的日期和時間。Sets the date and time that the specified file was last written to.

SetLastWriteTimeUtc(String, DateTime) SetLastWriteTimeUtc(String, DateTime) SetLastWriteTimeUtc(String, DateTime) SetLastWriteTimeUtc(String, DateTime)

設定指定檔案上次寫入的日期和時間,格式為國際標準時間 (UTC)。Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.

WriteAllBytes(String, Byte[]) WriteAllBytes(String, Byte[]) WriteAllBytes(String, Byte[]) WriteAllBytes(String, Byte[])

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

WriteAllBytesAsync(String, Byte[], CancellationToken) WriteAllBytesAsync(String, Byte[], CancellationToken) WriteAllBytesAsync(String, Byte[], CancellationToken) WriteAllBytesAsync(String, Byte[], CancellationToken)
WriteAllLines(String, String[], Encoding) WriteAllLines(String, String[], Encoding) WriteAllLines(String, String[], Encoding) WriteAllLines(String, String[], Encoding)

建立新檔案,使用指定的編碼方式將指定的字串陣列寫入檔案,然後關閉檔案。Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

WriteAllLines(String, IEnumerable<String>, Encoding) WriteAllLines(String, IEnumerable<String>, Encoding) WriteAllLines(String, IEnumerable<String>, Encoding) WriteAllLines(String, IEnumerable<String>, Encoding)

使用指定編碼方式建立新檔案,並將字串的集合寫入檔案,然後關閉檔案。Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.

WriteAllLines(String, String[]) WriteAllLines(String, String[]) WriteAllLines(String, String[]) WriteAllLines(String, String[])

建立新檔案,將指定的字串陣列寫入檔案,然後關閉檔案。Creates a new file, write the specified string array to the file, and then closes the file.

WriteAllLines(String, IEnumerable<String>) WriteAllLines(String, IEnumerable<String>) WriteAllLines(String, IEnumerable<String>) WriteAllLines(String, IEnumerable<String>)

建立新檔案,並於檔案中寫入字串的集合,然後關閉檔案。Creates a new file, writes a collection of strings to the file, and then closes the file.

WriteAllLinesAsync(String, IEnumerable<String>, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, CancellationToken)
WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken)
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) 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.

WriteAllTextAsync(String, String, CancellationToken) WriteAllTextAsync(String, String, CancellationToken) WriteAllTextAsync(String, String, CancellationToken) WriteAllTextAsync(String, String, CancellationToken)
WriteAllTextAsync(String, String, Encoding, CancellationToken) WriteAllTextAsync(String, String, Encoding, CancellationToken) WriteAllTextAsync(String, String, Encoding, CancellationToken) WriteAllTextAsync(String, String, Encoding, CancellationToken)

適用於

另請參閱