0% found this document useful (0 votes)
13 views1 page

complete-reference-vb_net_58

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

complete-reference-vb_net_58

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SeekOrigin Enumeration

After the words are added, the file stream is closed and the information is automatically saved. The following
example appends to a file. Instead of the words inserted at the top of the file, they are appended at the end of
the text in the file:

Public Sub AddWords(ByVal source As String, ByVal neword As String)


Dim aFile As New FileStream(source, IO.FileMode.OpenOrCreate, _
FileAccess.Write)
Dim wordadder As StreamWriter = New StreamWriter(aFile)
'Gets new words to append to the end of the file.
wordadder.BaseStream.Seek(0, SeekOrigin.End)
wordadder.WriteLine("neword)
wordadder.Close()
End Sub

In the preceding examples, there is a slight difference at the Seek calls. The change is the BaseStream.Seek
method of the StreamWriter object that we created. I did a SeekOrigin.End on the StreamWriter object,
and since the mode that we opened the file in was FileMode.OpenOrCreate, it opened the existing file that
we created earlier. This means that the subsequent executions of the second example will append the data to
the file. You can easily change the position by changing the SeekOrigin enumeration. This is demonstrated as
follows:

Public Sub AddWords(ByVal source As String, ByVal neword As String)


Dim aFile As New FileStream(source, IO.FileMode.OpenOrCreate, _
FileAccess.Write)
Dim wordadder As StreamWriter = New StreamWriter(aFile)
'Gets new words to append to the end of the file.
wordadder.BaseStream.Seek(0, SeekOrigin.Begin)
wordadder.WriteLine("neword")
wordadder.Close()
End Sub

If I were to rerun the earlier example, the file would get overwritten, because I am not moving the pointer to
the end of the file; I am just writing as soon as I open the file.

SeekOrigin Enumeration

The SeekOrigin construct is used by the Seek methods of the Stream and "writer" classes described in this
chapter. Seek methods take an offset parameter that is relative to the position specified by SeekOrigin. Table
15−26 lists the SeekOrigin constants.

Table 15−26: Constants of the SeekOrigin Enumeration

Member Purpose
Begin Specifies the beginning of a stream
Current Specifies the current position within a stream
End Specifies the end of a stream
This following example shows a use of SeekOrigin with BaseStream and Seek to set the file pointer of the
underlying stream to the beginning:

Dim aFile As New FileStream(source, FileMode.OpenOrCreate,_


FileAccess.Read)
Dim wordReader As New StreamReader(aFile)

542

You might also like