Export (0) Print
Expand All

FileMode Enumeration

Specifies how the operating system should open a file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum FileMode

Member nameDescription
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkCreateNewSpecifies that the operating system should create a new file. This requires FileIOPermissionAccess.Write. If the file already exists, an IOException is thrown.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkCreateSpecifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires FileIOPermissionAccess.Write. System.IO.FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. If the file already exists but is a hidden file, an UnauthorizedAccessException is thrown.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkOpenSpecifies that the operating system should open an existing file. The ability to open the file is dependent on the value specified by FileAccess. A System.IO.FileNotFoundException is thrown if the file does not exist.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkOpenOrCreateSpecifies 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 the file access is FileAccess.Write then FileIOPermissionAccess.Write is required. If the file is opened with FileAccess.ReadWrite, both FileIOPermissionAccess.Read and FileIOPermissionAccess.Write are required. If the file access is FileAccess.Append, then FileIOPermissionAccess.Append is required.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkTruncateSpecifies 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.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAppendOpens 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. Attempting to seek to a position before the end of the file will throw an IOException and any attempt to read fails and throws an NotSupportedException.

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.

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.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: Windows CE cannot store more than 999 files in a directory when the files share the same short file name (that is, the eight-character name, period (.), and 3-character extension). The workaround is to ensure that the short file names are different. For example, if the files are named Longfilename0001.txt through Longfilename1000.txt, place the number at the beginning of the file name instead of at the end.

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

FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft