FileMode FileMode FileMode FileMode Enum

定义

指定操作系统打开文件的方式。Specifies how the operating system should open a file.

public enum class FileMode
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum FileMode
type FileMode = 
Public Enum FileMode
继承
属性

字段

Append Append Append Append 6

若存在文件,则打开该文件并查找到文件尾,或者创建一个新文件。Opens the file if it exists and seeks to the end of the file, or creates a new file. 这需要 Append 权限。This requires Append permission. FileMode.Append 只能与 FileAccess.Write 一起使用。FileMode.Append can be used only in conjunction with FileAccess.Write. 试图查找文件尾之前的位置时会引发 IOException 异常,并且任何试图读取的操作都会失败并引发 NotSupportedException 异常。Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.

Create Create Create Create 2

指定操作系统应创建新文件。Specifies that the operating system should create a new file. 如果文件已存在,它将被覆盖。If the file already exists, it will be overwritten. 这需要 Write 权限。This requires Write permission. FileMode.Create 等效于这样的请求:如果文件不存在,则使用 CreateNew;否则使用 TruncateFileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. 如果该文件已存在但为隐藏文件,则将引发 UnauthorizedAccessException异常。If the file already exists but is a hidden file, an UnauthorizedAccessException exception is thrown.

CreateNew CreateNew CreateNew CreateNew 1

指定操作系统应创建新文件。Specifies that the operating system should create a new file. 这需要 Write 权限。This requires Write permission. 如果文件已存在,则将引发 IOException异常。If the file already exists, an IOException exception is thrown.

Open Open Open Open 3

指定操作系统应打开现有文件。Specifies that the operating system should open an existing file. 打开文件的能力取决于 FileAccess 枚举所指定的值。The ability to open the file is dependent on the value specified by the FileAccess enumeration. 如果文件不存在,引发一个 FileNotFoundException 异常。A FileNotFoundException exception is thrown if the file does not exist.

OpenOrCreate OpenOrCreate OpenOrCreate OpenOrCreate 4

指定操作系统应打开文件(如果文件存在);否则,应创建新文件。Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. 如果用 FileAccess.Read 打开文件,则需要 Read权限。If the file is opened with FileAccess.Read, Read permission is required. 如果文件访问为 FileAccess.Write,则需要 Write权限。If the file access is FileAccess.Write, Write permission is required. 如果用 FileAccess.ReadWrite 打开文件,则同时需要 ReadWrite权限。If the file is opened with FileAccess.ReadWrite, both Read and Write permissions are required.

Truncate Truncate Truncate Truncate 5

指定操作系统应打开现有文件。Specifies that the operating system should open an existing file. 该文件被打开时,将被截断为零字节大小。When the file is opened, it should be truncated so that its size is zero bytes. 这需要 Write 权限。This requires Write permission. 尝试从使用 FileMode.Truncate 打开的文件中进行读取将导致 ArgumentException 异常。Attempts to read from a file opened with FileMode.Truncate cause an ArgumentException exception.

示例

以下FileStream构造函数将打开一个现有文件 (FileMode.Open)。The following FileStream constructor opens an existing file (FileMode.Open).

FileStream^ s2 = gcnew FileStream( name, FileMode::Open, FileAccess::Read, FileShare::Read );
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
Dim s2 As New FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read)

注解

创建文件和文本写入文件的示例,请参阅如何:向文件写入文本For an example of creating a file and writing text to a file, see How to: Write Text to a File. 从文件读取文本的示例,请参阅如何:从文件读取文本For an example of reading text from a file, see How to: Read Text from a File. 读取和写入二进制文件的示例,请参阅如何:对新创建的数据文件读取和写入For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.

一个FileMode许多的构造函数中指定参数FileStreamIsolatedStorageFileStream,然后在Open方法FileFileInfo来控制如何打开文件。A FileMode parameter is specified in many of the constructors for FileStream, IsolatedStorageFileStream, and in the Open methods of File and FileInfo to control how a file is opened.

FileMode 参数控制是否覆盖文件,创建、 打开,或它们的组合。FileMode parameters control whether a file is overwritten, created, opened, or some combination thereof. 使用Open打开现有文件。Use Open to open an existing file. 若要追加到文件,请使用AppendTo append to a file, use Append. 若要将文件截断或创建一个文件,如果不存在,请使用CreateTo truncate a file or create a file if it doesn't exist, use Create.

适用于

另请参阅