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)

创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件或新文件(如果指定文件不存在)。Creates 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.

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)

适用于

另请参阅