Sunday, August 3, 2008

F

Files in VB .NET Working with Files
File handling in Visual Basic is based on System.IO namespace with a class library that supports string, character and file manipulation. These classes contain properties, methods and events for creating, copying, moving, and deleting files. Since both strings and numeric data types are supported, they also allow us to incorporate data types in files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.
FileStream Class
This class provides access to standard input and output files. We use the members of FileAccess, FileMode and FileShare Enumerations with the constructors of this class to create or open a file. After a file is opened it's FileStream object can be passed to the Binary Reader, BinaryWriter, Streamreader and StreamWriter classes to work with the data in the file. We can also use the FileStreamSeek method to move to various locations in a file which allows to break a file into records each of the same length.
StreamReader and StreamWriter Class
The StreamReader and StreamWriter classes enables us to read or write a sequential stream of characters to or from a file.
BinaryReader and BinaryWriter Class
The BinaryReader and BinaryWriter classes enable us to read and write binary data, raw 0's and 1's, the form in which data is stored on the computer.
The following examples puts some code to work with textual data using FileStream and StreamReader and StreamWriter classes.
Code to create a File
Imports System.IO'NameSpace required to be imported to work with files

Public Class Form1 Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e_As System.EventArgs) Handles MyBase.Load

Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)

'declaring a FileStream and creating a word document file named file with

'access mode of writing

Dim s as new StreamWriter(fs)

'creating a new StreamWriter and passing the filestream

object fs as arguments.BaseStream.Seek(0,SeekOrigin.End)

'the seek method is used to move the cursor to next position to avoid text to be

'overwrittens.WriteLine("This is an example of using file handling concepts in VB .NET.")s.WriteLine("This concept is interesting.")

'writing text to the newly created

files.Close()

'closing the file

End Sub

End Class
The default location where the files we create are saved is the bin directory of the Windows Application with which we are working. The image below displays that.
Code to create a file and read from it
Drag a Button and a RichTextBox control onto the form. Paste the following code which is shown below.
Imports System.IO'NameSpace required to be imported to work with files

Public Class Form1 Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal....., Byval.....)Handles Button1.Click

Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)

'declaring a FileStream and creating a document file named file with 'access mode of writing

Dim s as new StreamWriter(fs)

'creating a new StreamWriter and passing the filestream

object fs as arguments.WriteLine("This is an example of using file handling concepts in VB .NET.")

s.WriteLine("This concept is interesting.")

'writing text to the newly created

files.Close()

'closing the file

fs=New FileStream("file.doc",FileMode.Open,FileAccess.Read)

'declaring a FileStream to open the file named file.doc with access mode of reading

Dim d as new StreamReader(fs)

'creating a new StreamReader and passing the filestream

object fs as argumentd.BaseStream.Seek(0,SeekOrigin.Begin)

'Seek method is used to move the cursor to different positions in a file, in this code, to 'the beginning

while d.peek()>-1

'peek method of StreamReader object tells how much more data is left in the file

RichTextbox1.Text &= d.readLine()

'displaying text from doc file in the RichTextBox

End while

d.close()

End Sub

No comments: