FILESTREAM
The FileStream class provides a stream representation of a file.
The FileStream class’s parent class Stream defines most of its properties and methods. See the preceding section “Stream” for descriptions of those properties and methods.
FileStream adds two useful new properties to those it inherits from Stream. First, IsAsync returns True if the FileStream was opened asynchronously. Second, the Name property returns the filename passed into the object’s constructor.
The class also adds two new useful methods to those it inherits from Stream. The Lock method locks the file, so other processes can read it but not modify it. Unlock removes a previous lock.
Overloaded versions of the FileStream class’s constructor let you specify the following:
- Filename or handle
- File mode (Append, Create, CreateNew, Open, OpenOrCreate, or Truncate)
- Access mode (Read, Write, or ReadWrite)
- File sharing (Inheritable, which allows child processes to inherit the file handle, None, Read, Write, or ReadWrite)
- Buffer size
- File options (Asynchronous, DeleteOnClose, Encrypted, None, RandomAccess, SequentialScan, or WriteThrough)
Example program FileStreamWrite uses the following code to create a file. It creates a file and uses a Universal Transformation Format (UTF) UTF8Encoding object to convert a string into an array of bytes. It writes the bytes into the file and then closes the FileStream.
Dim file_name As String = Application.StartupPath & "\test.txt" Using file_stream As New FileStream(file_name, ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access