FileStream

FileStream


Represents a file as a Stream.


Remarks

A FileStream object cannot be created directly. In order to create a new FileStream object, use the Cor.NewFileStream method.

The FileStream can be used to create and modify files saved to disk. Through the stream the file can be navigated to specified byte positions within the file and the data can be read or written. Writing data will overwrite the data currently in the file at that location. As data is read or written, the file pointer is advanced the number of bytes read or written.

Additionally, and existing handle to a file, pipe, or mailslot can have a stream created around it. These streams are not seekable, meaning the position within the stream cannot be set manually. Reading and writing to these streams still cause the pointer to advance the appropriate number of bytes.

The FileStream object buffers reads and writes to the underlying stream for improved performance. It is assumed that several reads or several writes will occur repeatedly. This allows a single buffer to be used between reading and writing. If reading begins and there is data written, the data is flushed out to the stream to ensure the stream remains in sync. The buffer is then filled with BufferSize bytes for a quicker read.

Example

' This example creates a new file and writes an array'' of bytes containing the encoded string data. Once'' the file is written to, it is re-opened and read from'' recreating the original string for display.Private Sub Main()    Dim fs As FileStream    Dim b() As Byte        '' Encode a string using the default encoding scheme.    b = Encoding.Default.GetBytes("Hello")        '' Open a text file. If the file already exits, it    '' will be overwritten.    Set fs = NewFileStream("data.txt", FileMode.Create)        '' Write the encoded bytes to the file stream    fs.WriteBlock b, 0, cArray.GetLength(b)    fs.CloseStream        '' Re-open the the file using a new FileStream object.    Set fs = NewFileStream("data.txt", FileMode.OpenExisting)        '' Resize the byte array to hold all the bytes in the file.    ReDim b(0 To fs.Length - 1)        '' Read in all bytes in the file.    fs.ReadBlock b, 0, fs.Length    fs.CloseStream        '' Decode the byte array back into a string    '' and display the string.    Console.WriteLine Encoding.Default.GetString(b)    Console.ReadLine    End Sub

See Also

Project VBCorLib Overview | Constructors | Stream

Implements:

IObject 
Stream 

Public:

Types:

NameDescription
 FileAccessThe accesses allowed to the FileStream. 
 FileModeThe modes in which to open a FileStream 
 FileShareHow the underlying stream can be shared with other processes. 
 NativeOverlapped 

Properties:

NameDescription
 CanRead (get)Returns an indication for the ability to read from the file stream. 
 CanSeek (get)Returns an indication for the ability to seek within the file stream. 
 CanTimeout (get)Returns if this object can timeout. 
 CanWrite (get)Returns an indication for the ability to write to the file stream. 
 Handle (get)Returns the handle to the underlying stream. 
 IsAsync (get)Returns whether or not the FileStream was opened up in asynchronous mode. 
 Length (get)The length of the current stream in bytes. 
 Name (get)Returns the name of the underlying stream. 
 Position (get)Returns the current position within the stream. 
 Position (let)Sets the current position within the stream to be read from or written to. 
 ReadTimeout (get)Returns the read timeout duration. 
 ReadTimeout (let)Sets the read timeout duration. 
 WriteTimeout (get)Returns the write timeout duration. 
 WriteTimeout (let)Sets the write timeout duration. 

Methods:

NameDescription
 BeginReadBegins an Asynchronous read operation (currently is only synchronous) 
 BeginWriteBegins an asynchronous buffer write. Currently the FileStream class does not support asynchronous buffer writing. 
 CloseStreamCloses the current stream, flushing any data that may need to be written to the stream. 
 EndReadSignifies the end of an asynchronous read from the stream. 
 EndWriteSignifies the end of an asynchronous write to the stream. 
 EqualsReturns a boolean indicating if the value and this object instance are the same instance. 
 FlushWrites any data that may be in the write buffer to the underlying stream. 
 GetHashCodeReturns a pseudo-unique number identifying this instance. 
 LockStreamLocks a portion of a file to prevent write access for other processes. 
 ReadBlockReads a specified number of bytes into the given array. 
 ReadByteReturns the next byte in the file stream starting at the current file position. 
 SeekPositionMoves the file pointer to a new position relative to a specified reference. 
 SetLengthSets the length of the stream to the specified length. 
 ToStringReturns a string representation of this object instance. 
 UnlockStreamUnlocks a portion of the file to allow write access to the stream for other processes. 
 WriteBlockWrites an array of bytes to the stream. 
 WriteByteWrites a single byte to the stream.