0% found this document useful (0 votes)
3 views

complete-reference-vb_net_57

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

complete-reference-vb_net_57

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

FileStream

Seek

The Seek method changes the current position in the stream to the value passed as the argument. Its signature
is as follows:

Overrides Public Function Seek( _


ByVal offset As Long, _
ByVal origin As SeekOrigin _
) As Long

The following list describes its parameters:

• offset A point relative to the origin from which to begin seeking.


• origin A value that specifies the beginning, the end, or the current position as a reference point for
origin, using a value of type SeekOrigin.
• Return Value You get back the new position in the stream.

Seek can cause the exceptions listed in Table 15−25.

You can use the CanSeek property to determine whether the current instance supports seeking. Also note that
seeking to any location beyond the length of the stream is supported. Set the position to one byte beyond the
end of the stream, as recommended by the SDK documentation to open a new file and write to it. This lets you
append to the file. Just remember that the position cannot be set to more than one byte beyond the end of the
stream.

Table 15−25: Exceptions That Can Be Generated on a Seek Operation

Exception Type Condition


IOException An I/O error occurred
NotSupportedException The stream does not support seeking. This can happen if the
FileStream is constructed from a pipe or console output.
ArgumentException Attempted seeking before the beginning of the stream or more than
one byte past the end of the stream
ObjectDisposedException Methods were called after the stream was closed
One of the classes you will find especially interesting is IsolatedStorageFileStream. This class supports the
streaming of data to isolated storage units, which are a form of private file systems that can contain files and
that can only be accessed by an owner, an application, or user.

Writing to a file stream object can be performed like the following examples which add words to the top of
the noisewords files. Existing words are pushed down:

Public Sub AddWords(ByVal fileandpath 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 add to the file.
wordadder.WriteLine("neword)
wordadder.Close()
End Sub

541

You might also like