The FileStream class

The FileStream class allows you to handle file processing. This is the lowest class deriving from Stream that you can use if you want to work with files.

In order to use a FileStream object, you must create an instance of it by using one of it's constructor.

The complete version of this constructor is:

public FileStream(
   string fileName,    // The name of the file
   FileMode mode,      // The mode of the file.
                       // May be Append, Create, CreateNew, OpenOrCreate, Truncate
   FileAccess access,  // The access to use for the file.
                       // May be Read, ReadWrite, Write.
   FileShare share,    // Specify the sharing mode.
                       // May be None, Read, ReadWrite, Write.
   int buffSize,       // The size of the buffer
   bool isAsync        // Should we access the FileStream asynchronously
);

As FileStream derives from Stream, it implements all the methods that Stream exposes and two more methods: Lock and Unlock.

These two methods allow to lock or unlock the access of a given byte range in the file. These two methods accepts the same parameters, the first one is a long and specify the position from which to begin locking or unlocking. The second one specify how many bytes should be locked or unlocked.

Advantages Drawbacks
 As it inherits from Stream, it may be used whenever a Stream is needed  Allows only byte and byte array reading/writing
 Allow locking/unlocking part of a file  As it can only write bytes, the conversion must be handled by the application

Now that we've described the FileStream class, we can go on to the FileStream helper classes: