complete-reference-vb_net_58
complete-reference-vb_net_58
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:
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:
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.
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:
542