A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

VB.Net I/O Classes

The System.IO namespace has various classes that are used for performing various operations with files, like creating and deleting files, reading from or writing to a file, closing a file, etc.

The following table shows some commonly used non-abstract classes in the System.IO namespace:

I/O Class Description
BinaryReader Reads primitive data from a binary stream.
BinaryWriter Writes primitive data in binary format.
BufferedStream A temporary storage for a stream of bytes.
Directory Helps in manipulating a directory structure.
DirectoryInfo Used for performing operations on directories.
DriveInfo Provides information for the drives.
File Helps in manipulating files.
FileInfo Used for performing operations on files.
FileStream Used to read from and write to any location in a file.
MemoryStream Used for random access of streamed data stored in memory.
Path Performs operations on path information.
StreamReader Used for reading characters from a byte stream.
StreamWriter Is used for writing characters to a stream.
StringReader Is used for reading from a string buffer.
StringWriter Is used for writing into a string buffer.

The FileStream Class

The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.

You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows:

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

For example, for creating a FileStream object F for reading a file named sample.txt:

Dim f1 As FileStream = New FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Parameter Description
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

Example:

The following program demonstrates use of the FileStream class:

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

When the above code is compiled and executed, it produces the following result:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

Advanced File Operations in VB.Net

The preceding example provides simple file operations in VB.Net. However, to utilize the immense powers of System.IO classes, you need to know the commonly used properties and methods of these classes.

We will discuss these classes and the operations they perform in the following sections. Please click the links provided to get to the individual sections:

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.

An object is a type of user interface element you create on a Visual Basic form by using a toolbox control. In fact, in Visual Basic, the form itself is an object. Every Visual Basic control consists of three important elements:

  • Properties which describe the object,

  • Methods cause an object to do something and

  • Events are what happens when an object does something.

Control Properties

All the Visual Basic Objects can be moved, resized or customized by setting their properties. A property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color.

Properties can be set at design time by using the Properties window or at run time by using statements in the program code.

Object. Property = Value

Where

  • Object is the name of the object you're customizing.

  • Property is the characteristic you want to change.

  • Value is the new property setting.

For example,

Form1.Caption = "Hello"

You can set any of the form properties using Properties Window. Most of the properties can be set or read during application execution. You can refer to Microsoft documentation for a complete list of properties associated with different controls and restrictions applied to them.

Control Methods

A method is a procedure created as a member of a class and they cause an object to do something. Methods are used to access or manipulate the characteristics of an object or a variable. There are mainly two categories of methods you will use in your classes:

  • If you are using a control such as one of those provided by the Toolbox, you can call any of its public methods. The requirements of such a method depend on the class being used.

  • If none of the existing methods can perform your desired task, you can add a method to a class.

For example, the MessageBox control has a method named Show, which is called in the code snippet below:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
	Handles Button1.Click
        MessageBox.Show("Hello, World")
    End Sub
End Class

Control Events

An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event. There are various types of events associated with a Form like click, double click, close, load, resize, etc.

Following is the default structure of a form Load event handler subroutine. You can see this code by double clicking the code which will give you a complete list of the all events associated with Form control:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 'event handler code goes here
End Sub

Here, 



Copyright www.learninghints.com