C# FileStream FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

Description

FileStream FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.

Syntax

FileStream.FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) has the following syntax.


public FileStream(
  string path,/*from   ww  w . j  ava2s. c o m*/
  FileMode mode,
  FileAccess access,
  FileShare share,
  int bufferSize,
  FileOptions options
)

Parameters

FileStream.FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) has the following parameters.

  • path - A relative or absolute path for the file that the current FileStream object will encapsulate.
  • mode - A constant that determines how to open or create the file.
  • access - 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 - A constant that determines how the file will be shared by processes.
  • bufferSize - 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 - A value that specifies additional file options.

Example

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


//w ww .j  av  a2 s. c om
using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;

class FileStreamExample
{
    public static void Main()
    {
        byte[] messageByte = Encoding.ASCII.GetBytes("from java2s.com");
        FileStream fWrite = new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None);
        fWrite.WriteByte((byte)messageByte.Length);
        fWrite.Write(messageByte, 0, messageByte.Length);
        fWrite.Close();

        FileStream fRead = new FileStream("test.txt", FileMode.Open);
        int length = (int)fRead.ReadByte();
        byte[] readBytes = new byte[length];
        fRead.Read(readBytes, 0, readBytes.Length);
        fRead.Close();
        Console.WriteLine(Encoding.ASCII.GetString(readBytes));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter