Export (0) Print
Expand All

FileStream Constructor (String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.

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

public FileStream(
	string path,
	FileMode mode,
	FileSystemRights rights,
	FileShare share,
	int bufferSize,
	FileOptions options,
	FileSecurity fileSecurity
)

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.

rights
Type: System.Security.AccessControl.FileSystemRights

A constant that determines the access rights to use when creating access and audit rules for the file.

share
Type: System.IO.FileShare

A constant that determines how the file will be shared by processes.

bufferSize
Type: System.Int32

A positive Int32 value greater than 0 indicating the buffer size. For bufferSize values between one and eight, the actual buffer size is set to eight bytes.

options
Type: System.IO.FileOptions

A constant that specifies additional file options.

fileSecurity
Type: System.Security.AccessControl.FileSecurity

A constant that determines the access control and audit security for the file.

ExceptionCondition
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.

ArgumentOutOfRangeException

bufferSize is negative or zero.

-or-

mode, access, or share contain an invalid value.

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 occurs, such as specifying FileMode.CreateNew and the file specified by path already exists.

-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.

-or-

Encrypted is specified for options, but file encryption is not supported on the current platform.

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.

PlatformNotSupportedException

The current operating system is not Windows NT or later.

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

Use this FileStream constructor to apply access rights at the point of creation of a file. To access or modify rights on an existing file, consider using the GetAccessControl and SetAccessControl methods.

The fileOptions parameter is used to provide access to more advanced operations that can be leveraged when creating a FileStream object.

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

NoteNote:

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 noteCaution:

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.

The following example writes data to a file and then reads the data using the FileStream object.

using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileStreamExample
    {
        public static void Main()
        {
            try
            {
                // Create a file and write data to it. 

                // Create an array of bytes.
                byte[] messageByte = Encoding.ASCII.GetBytes("Here is some data.");

                // Specify an access control list (ACL)
                FileSecurity fs = new FileSecurity();

                fs.AddAccessRule(new FileSystemAccessRule(@"DOMAINNAME\AccountName",
                                                            FileSystemRights.ReadData,
                                                            AccessControlType.Allow));

                // Create a file using the FileStream class.
                FileStream fWrite = new FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs);

                // Write the number of bytes to the file.
                fWrite.WriteByte((byte)messageByte.Length);

                // Write the bytes to the file.
                fWrite.Write(messageByte, 0, messageByte.Length);

                // Close the stream.
                fWrite.Close();


                // Open a file and read the number of bytes.

                FileStream fRead = new FileStream("test.txt", FileMode.Open);

                // The first byte is the string length. 
                int length = (int)fRead.ReadByte();

                // Create a new byte array for the data.
                byte[] readBytes = new byte[length];

                // Read the data from the file.
                fRead.Read(readBytes, 0, readBytes.Length);

                // Close the stream.
                fRead.Close();

                // Display the data.
                Console.WriteLine(Encoding.ASCII.GetString(readBytes));

                Console.WriteLine("Done writing and reading data.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
    }
}

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

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

Community Additions

ADD
Show:
© 2014 Microsoft