Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileMode Enumeration

Specifies how the operating system should open a file.

[Visual Basic]
<Serializable>
Public Enum FileMode
[C#]
[Serializable]
public enum FileMode
[C++]
[Serializable]
__value public enum FileMode
[JScript]
public
   Serializable
enum FileMode

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

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.

Members

Member name Description
Append

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

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 CreateNew; otherwise, use Truncate.
CreateNew

Supported by the .NET Compact Framework.

Specifies that the operating system should create a new file. This requires FileIOPermissionAccess.Write. If the file already exists, an IOException is thrown.
Open

Supported by the .NET Compact Framework.

Specifies that the operating system should open an existing file. The ability to open the file is dependent on the the value specified by FileAccess. A System.IO.FileNotFoundException is thrown if the file does not exist.
OpenOrCreate

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

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.

Example

The following FileStream constructor opens an existing file (FileMode.Open).

[Visual Basic] 
Dim s2 As New FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read)

[C#] 
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

[C++] 
FileStream* s2 = new FileStream(name, FileMode::Open, FileAccess::Read, FileShare::Read);

[JScript] 
var s2 : FileStream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.IO Namespace | File.Open | FileInfo.Open | FileStream | IsolatedStorageFileStream | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Show:
© 2014 Microsoft