System.IO.FileMode Enumeration

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Specifies how the operating system should open a file.
C# Syntax:
[Serializable]
public enum FileMode
Remarks
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 parameters control whether a file is overwritten, created, or opened, or some combination thereof. Use Open to open an existing file. To append to a file, use Append. To truncate a file or to create it if it does not exist, use Create.

Example
The following FileStream constructor opens an existing file (FileMode.Open).
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

    
See also:
System.IO Namespace | File.Open | FileInfo.Open | FileStream | IsolatedStorageFileStream

System.IO.FileMode Member List:

Public Fields
Append Opens the file if it exists and seeks to the end of the file, or creates a new file.FileMode.Append can only be used in conjunction with FileAccess.Write. Any attempt to read fails and throws an ArgumentException.
Create Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires FileIOPermissionAccess.Write and FileIOPermissionAccess.Append.System.IO.FileMode.Create is equivalent to requesting that if the file does not exist, use FileMode.CreateNew; otherwise, use FileMode.Truncate.
CreateNew Specifies that the operating system should create a new file. This requires FileIOPermissionAccess.Read and FileIOPermissionAccess.Append. If the file already exists, an IOException is thrown.
Open Specifies that the operating system should open an existing file. This requires FileIOPermissionAccess.Read. A FileNotFoundException is thrown if the file does not exist.
OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. If the file is opened with FileAccess.Read, FileIOPermissionAccess.Read is required. If file access is FileAccess.ReadWrite and the file exists, FileIOPermissionAccess.Write is required. If file access is FileAccess.ReadWrite and the file does not exist, FileIOPermissionAccess.Append is required in addition to Read and Write.
Truncate Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes. This requires FileIOPermissionAccess.Write. Attempts to read from a file opened with Truncate cause an exception.

Hierarchy:


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.