System.IO.File.Open Method

Opens a System.IO.FileStream on the specified path with read/write access.

Syntax

public static FileStream Open (string path, FileMode mode)

Parameters

mode
A System.IO.FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
path
The file to open.
mode
A System.IO.FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.

Returns

A System.IO.FileStream opened in the specified mode and path, with read/write access and not shared.

Exceptions

TypeReason
System.Security.SecurityExceptionThe caller does not have the required permission.
ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
System.IO.FileNotFoundException path was not found.
ArgumentNullException path is null.
ArgumentOutOfRangeException mode specified an invalid value.
System.IO.DirectoryNotFoundExceptionThe directory information specified in path was not found.
System.IO.IOExceptionAn I/O error occurred while opening the file.
UnauthorizedAccessExceptionThe caller does not have the required permission.
System.IO.PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.

Remarks

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.

For a list of common I/O tasks, see Common I/O Tasks.

Permissions

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0