FileStream

FileStream


Represents a file as a Stream.


Implements:

IObject 
Stream 

Public:

Types:

NameDescription
 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.  

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.  
 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.  
 SafeFileHandle (get) Returns the handle to the underlying stream.  
 WriteTimeout (get) Returns the write timeout duration.  
 WriteTimeout (let) Sets the write timeout duration.  

Methods:

NameDescription
 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.  
 CopyTo Reads the bytes from the current stream and writes them to another 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.  

Remarks

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 CorLib Overview

Class FileStream Overview

Constructors

Stream