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.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")
           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 OpenTextCreateTextCreate等特定成员。For 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#中, "c:\MyDir\MyFile.txt"或在 Visual Basic 中。"c:\\\MyDir\\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\\MyDir"在C#中, "c:\MyDir"或在 Visual Basic 中。"c:\\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\\MySubdir"在C#中, "MyDir\MySubDir"或在 Visual Basic 中。"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)

以异步形式向一个文件中追加行,然后关闭该文件。Asynchronously 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.

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

使用指定的编码以异步形式向一个文件中追加行,然后关闭该文件。Asynchronously 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.

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 using the specified encoding, 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)

以异步方式打开文件或创建文件(如果文件尚不存在),将指定的字符串追加到该文件,然后关闭文件。Asynchronously opens a file or creates a file if it does not already exist, appends the specified string to the file, and then closes the file.

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

以异步方式打开文件或创建文件(如果文件尚不存在),使用指定的编码将指定的字符串追加到该文件,然后关闭文件。Asynchronously opens a file or creates the file if it does not already exist, appends the specified string to the file using the specified encoding, and then closes the file.

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 a file in the specified path, specifying a buffer size.

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

创建或覆盖指定路径中的文件,指定缓冲区大小和一个描述如何创建或覆盖该文件的选项。Creates or overwrites a file in the specified path, specifying a buffer size and options that describe 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 a file in the specified path, specifying a buffer size, options that describe how to create or overwrite the file, and a value that determines the access control and audit security for the file.

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)

将指定文件移动到新位置,提供指定新文件名和覆盖目标文件(如果它已存在)的选项。Moves a specified file to a new location, providing the options to specify a new file name and to overwrite the destination file if it already exists.

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)

以异步形式打开一个二进制文件,将文件的内容读入一个字节数组,然后关闭该文件。Asynchronously opens a binary file, reads the contents of the file into a byte array, and then closes the file.

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)

以异步形式打开一个文本文件,读取文件的所有行,然后关闭该文件。Asynchronously opens a text file, reads all lines of the file, and then closes the file.

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

以异步形式打开一个文本文件,使用指定的编码读取文件的所有行,然后关闭该文件。Asynchronously opens a text file, reads all lines of the file with the specified encoding, and then closes the file.

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)

以异步形式打开一个文本文件,读取文件中的所有文本,然后关闭此文件。Asynchronously opens a text file, reads all the text in the file, and then closes the file.

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

以异步形式打开一个文本文件,使用指定的编码读取文件的所有文本,然后关闭该文件。Asynchronously opens a text file, reads all text in the file with the specified encoding, and then closes the file.

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)

以异步形式创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。Asynchronously 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.

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)

以异步形式创建一个新文件,在其中写入指定的行,然后关闭该文件。Asynchronously creates a new file, writes the specified lines to the file, and then closes the file.

WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken) WriteAllLinesAsync(String, IEnumerable<String>, Encoding, CancellationToken)

以异步形式创建一个新文件,使用指定编码在其中写入指定的行,然后关闭该文件。Asynchronously creates a new file, write the specified lines to the file by using the specified encoding, and then closes the file.

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)

以异步形式创建一个新文件,在其中写入指定的字符串,然后关闭该文件。Asynchronously 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.

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

以异步形式创建一个新文件,使用指定编码在其中写入指定的字符串,然后关闭该文件。Asynchronously 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.

适用于

另请参阅