Menu

VB.NET TUTORIALS - VB.Net - File Handling

VB.Net - File Handling

ADVERTISEMENTS

VB.Net I/O Classes

I/O ClassDescription
BinaryReaderReads primitive data from a binary stream.
BinaryWriterWrites primitive data in binary format.
BufferedStreamA temporary storage for a stream of bytes.
DirectoryHelps in manipulating a directory structure.
DirectoryInfoUsed for performing operations on directories.
DriveInfoProvides information for the drives.
FileHelps in manipulating files.
FileInfoUsed for performing operations on files.
FileStreamUsed to read from and write to any location in a file.
MemoryStreamUsed for random access of streamed data stored in memory.
PathPerforms operations on path information.
StreamReaderUsed for reading characters from a byte stream.
StreamWriterIs used for writing characters to a stream.
StringReaderIs used for reading from a string buffer.
StringWriterIs used for writing into a string buffer.

ADVERTISEMENTS

The FileStream Class

ParameterDescription
FileMode

The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are:

  • Append: It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.

  • Create: It creates a new file.

  • CreateNew: It specifies to the operating system that it should create a new file.

  • Open: It opens an existing file.

  • OpenOrCreate: It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.

  • Truncate: It opens an existing file and truncates its size to zero bytes.

FileAccess

FileAccess enumerators have members: Read, ReadWrite and Write.

FileShare

FileShare enumerators have the following members:

  • Inheritable: It allows a file handle to pass inheritance to the child processes

  • None: It declines sharing of the current file

  • Read: It allows opening the file for reading

  • ReadWrite: It allows opening the file for reading and writing

  • Write: It allows opening the file for writing

ADVERTISEMENTS

Advanced File Operations in VB.Net

Topic and Description
Reading from and Writing into Text files
It involves reading from and writing into text files. The StreamReader and StreamWriter classes help to accomplish it.
Reading from and Writing into Binary files
It involves reading from and writing into binary files. The BinaryReader and BinaryWriter classes help to accomplish this.
Manipulating the Windows file system
It gives a VB.Net programmer the ability to browse and locate Windows files and directories.

The FileStream Class

Dim <object_name> As FileStream = New FileStream(<file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>)

Dim f1 As FileStream = New FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite)

Example:

Imports System.IO
Module fileProg
   Sub Main()
      Dim f1 As FileStream = New FileStream("test.dat", _
              FileMode.OpenOrCreate, FileAccess.ReadWrite)
      Dim i As Integer
      For i = 0 To 20
          f1.WriteByte(CByte(i))
      Next i
      f1.Position = 0
      For i = 0 To 20
          Console.Write("{0} ", f1.ReadByte())
      Next i
      f1.Close()
      Console.ReadKey()
   End Sub
End Module