This topic has not yet been rated - Rate this topic

FileStream Constructor (String, FileMode, FileAccess, FileShare)

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write permission, and sharing permission.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
public FileStream(
	string path,
	FileMode mode,
	FileAccess access,
	FileShare share
)

Parameters

path
Type: System.String
A relative or absolute path for the file that the current FileStream object will encapsulate.
mode
Type: System.IO.FileMode
A constant that determines how to open or create the file.
access
Type: System.IO.FileAccess
A constant that determines how the file can be accessed by the FileStream object. This gets the CanRead and CanWrite properties of the FileStream object. CanSeek is true if path specifies a disk file.
share
Type: System.IO.FileShare
A constant that determines how the file will be shared by processes.
Exception Condition
ArgumentNullException

path is null.

ArgumentException

path is an empty string (""), contains only white space, or contains one or more invalid characters.

-or-

path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.

NotSupportedException

path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.

ArgumentException

path is an empty string (""), contains only white space, or contains one or more invalid characters.

FileNotFoundException

The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist. The file must already exist in these modes.

IOException

An I/O error, such as specifying FileMode.CreateNew when the file specified by path already exists, occurred.

-or-

The system is running Windows 98 or Windows 98 Second Edition and share is set to FileShare.Delete.

-or-

The stream has been closed.

SecurityException

The caller does not have the required permission.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

UnauthorizedAccessException

The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

ArgumentOutOfRangeException

mode contains an invalid value.

The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The constructor is given read/write access to the file, and it is opened sharing Read access (that is, requests to open the file for writing by this or another process will fail until the FileStream object has been closed, but read attempts will succeed). The buffer size is set to the default size of 8192 bytes (8 KB).

Note Note

path is not required to be a file stored on disk; it can be any part of a system that supports access through streams. For example, depending on the system, this class can access a physical device.

CanSeek is true for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the CanSeek property on the resulting FileStream is false. For additional information, see CanSeek.

Caution note Caution

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

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

This code example is part of a larger example provided for the Lock method.


using(FileStream fileStream = new FileStream(
    "Test#@@#.dat", FileMode.OpenOrCreate, 
    FileAccess.ReadWrite, FileShare.ReadWrite))


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Reading file while writing to it

In Windows it's possible to open file for reading, while the file is still being written to. Notepad.exe is a perfect example. However, in c# regular StreamReader will not open file that is already opened for writing. Instead you must use FileStream and StreamReader combination.


Following code works:

string tempFileName = Path.GetTempFileName();
using (StreamWriter sw = new StreamWriter(tempFileName, true))
using (FileStream fs = new FileStream(tempFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) // Must be FileShare.ReadWrite, FileShare.Read doesn't work for some odd reason.
using (StreamReader sr = new StreamReader(fs))
{
sw.WriteLine("ttt");
sw.Flush();
Console.WriteLine(sr.ReadToEnd());
}

Following code will throw IOException:

string tempFileName = Path.GetTempFileName();
using (StreamWriter sw = new StreamWriter(tempFileName, true))
using (StreamReader sr = new StreamReader(tempFileName)) // IOException is thrown here
{
sw.WriteLine("ttt");
sw.Flush();
Console.WriteLine(sr.ReadToEnd());
}

What about the asynchronous I/O option?
There is an option for asynchronous I/O in one of the FileStream constructor overloads, but nothing is mentioned here about the default settings for asynchronous I/O for this constructor.