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

Description

FileStream FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, buffer size, and synchronous or asynchronous state.

Syntax

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


public FileStream(
  string path,/*  w w  w.  j av a 2s.  c  o  m*/
  FileMode mode,
  FileAccess access,
  FileShare share,
  int bufferSize,
  bool useAsync
)

Parameters

FileStream.FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 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.
  • useAsync - Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods perform better on large reads or writes, but they might be much slower for small reads or writes. If the application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.

Example

The following code example shows how to asynchronously write data to a file and then verify that the data was written correctly.


using System;//from   ww  w.  ja  v  a 2  s  . c  o  m
using System.IO;
using System.Threading;

class FStream
{
    static void Main()
    {
        ManualResetEvent manualEvent = new ManualResetEvent(false);
        byte[] writeArray = new byte[100];
        new Random().NextBytes(writeArray);

        FileStream fStream = new FileStream("Test.dat", FileMode.Create, 
            FileAccess.ReadWrite, FileShare.None, 4096, true);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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