| FileStream |
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.
' 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 SubSee Also
Project VBCorLib Overview | Constructors | Stream
| IObject | |
| Stream |
| Name | Description |
|---|---|
FileAccess | The accesses allowed to the FileStream. |
FileMode | The modes in which to open a FileStream |
FileShare | How the underlying stream can be shared with other processes. |
NativeOverlapped |
| Name | Description |
|---|---|
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. |
| Name | Description |
|---|---|
BeginRead | Begins an Asynchronous read operation (currently is only synchronous) |
BeginWrite | Begins an asynchronous buffer write. Currently the FileStream class does not support asynchronous buffer writing. |
CloseStream | Closes the current stream, flushing any data that may need to be written to the stream. |
EndRead | Signifies the end of an asynchronous read from the stream. |
EndWrite | Signifies the end of an asynchronous write to the stream. |
Equals | Returns a boolean indicating if the value and this object instance are the same instance. |
Flush | Writes any data that may be in the write buffer to the underlying stream. |
GetHashCode | Returns a pseudo-unique number identifying this instance. |
LockStream | Locks a portion of a file to prevent write access for other processes. |
ReadBlock | Reads a specified number of bytes into the given array. |
ReadByte | Returns the next byte in the file stream starting at the current file position. |
SeekPosition | Moves the file pointer to a new position relative to a specified reference. |
SetLength | Sets the length of the stream to the specified length. |
ToString | Returns a string representation of this object instance. |
UnlockStream | Unlocks a portion of the file to allow write access to the stream for other processes. |
WriteBlock | Writes an array of bytes to the stream. |
WriteByte | Writes a single byte to the stream. |