Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Specifies how the operating system should open a file.
public enum class FileMode
public enum FileMode
[System.Serializable]
public enum FileMode
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FileMode
type FileMode =
[<System.Serializable>]
type FileMode =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FileMode =
Public Enum FileMode
Name | Value | Description |
---|---|---|
CreateNew | 1 | Specifies that the operating system should create a new file. This requires Write permission. If the file already exists, an IOException exception is thrown. |
Create | 2 | Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires Write permission. |
Open | 3 | Specifies that the operating system should open an existing file. The ability to open the file is dependent on the value specified by the FileAccess enumeration. A FileNotFoundException exception is thrown if the file does not exist. |
OpenOrCreate | 4 | 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 |
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. This requires Write permission. Attempts to read from a file opened with |
Append | 6 | Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires Append permission. |
The following FileStream
constructor opens an existing file (FileMode.Open
).
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.
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, opened, or some combination thereof. Use Open
to open an existing file. To append to a file, use Append
. To truncate a file or create a file if it doesn't exist, use Create
.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in