Example

[Visual Basic, C#, C++] This example shows a use of SeekOrigin with BaseStream and Seek to set the file pointer of the underlying stream to the beginning.

[Visual Basic]
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim fs As New FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read)
' Create a character reader.
Dim w As New StreamReader(fs)
' Set the StreamReader file pointer to the end.
w.BaseStream.Seek(0, SeekOrigin.End)
End Sub
End Class
[C#]
using System;
using System.IO;
public class Test
{
public static void Main()
{
FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
FileAccess.Read);
// Create a character reader.
StreamReader w = new StreamReader(fs);
// Set the StreamReader file pointer to the end.
w.BaseStream.Seek(0, SeekOrigin.End);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main()
{
FileStream* fs = new FileStream(S"log.txt", FileMode::OpenOrCreate,
FileAccess::Read);
// Create a character reader.
StreamReader* w = new StreamReader(fs);
// Set the StreamReader file pointer to the end.
w->BaseStream->Seek(0, SeekOrigin::End);
}
[Visual Basic]
' System.IO.SeekOrigin.Current
' The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
' 'FileStream' object with the file access option as writer. It creates a 'StreamWriter' object
' to write the data into the file. After writing the data successfully it reads the
' first five characeters from the file. It increments the file pointer to one character
' by using the filestream seek method. After that it displays the next five characters.
Imports System
Imports System.IO
Imports System.Text
Class SeekOrigin_BeginEnd
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Sub Main(args() As [String])
' Create a 'FileStream' object.
Dim myFileStream As New FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Write)
' Create a 'StreamWriter' to write the data into the file.
Dim myStreamWriter As New StreamWriter(myFileStream)
myStreamWriter.WriteLine("James Harry Robart")
' Update the 'StreamWriter'.
myStreamWriter.Flush()
' Close the 'StreamWriter' and FileStream.
myStreamWriter.Close()
myFileStream.Close()
Dim myFileStream1 As New FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Read)
' Set the file pointer to the begin.
myFileStream1.Seek(0, SeekOrigin.Begin)
Dim myByteArray(5) As [Byte]
' Read the first five characeters.
myFileStream1.Read(myByteArray, 0, 5)
Dim myEncoding As New ASCIIEncoding()
Console.WriteLine("The Contents of the file are :")
Console.WriteLine(myEncoding.GetString(myByteArray))
' Increment the file pointer from CurrentPosition by one character.
myFileStream1.Seek(1, SeekOrigin.Current)
' Read the next five characeters.
myFileStream1.Read(myByteArray, 0, 5)
Console.WriteLine(myEncoding.GetString(myByteArray))
' Close the 'FileStream'.
myFileStream1.Close()
End Sub 'Main
End Class 'SeekOrigin_BeginEnd
[C#]
// System.IO.SeekOrigin.Current
/*
The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
'FileStream' object with the file access option as writer. It creates a 'StreamWriter' object
to write the data into the file. After writing the data successfully it reads the
first five characeters from the file. It increments the file pointer to one character
by using the filestream seek method. After that it displays the next five characters.
*/
using System;
using System.IO;
using System.Text;
class SeekOrigin_BeginEnd
{
public static void Main(String[] args)
{
// Create a 'FileStream' object.
FileStream myFileStream = new FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Write);
// Create a 'StreamWriter' to write the data into the file.
StreamWriter myStreamWriter = new StreamWriter(myFileStream);
myStreamWriter.WriteLine("James Harry Robart");
// Update the 'StreamWriter'.
myStreamWriter.Flush();
// Close the 'StreamWriter' and FileStream.
myStreamWriter.Close();
myFileStream.Close();
FileStream myFileStream1 = new FileStream("Names.txt", FileMode.OpenOrCreate, FileAccess.Read);
// Set the file pointer to the begin.
myFileStream1.Seek(0, SeekOrigin.Begin);
Byte[] myByteArray = new Byte[5];
// Read the first five characeters.
myFileStream1.Read(myByteArray,0,5);
ASCIIEncoding myEncoding = new ASCIIEncoding();
Console.WriteLine("The Contents of the file are :");
Console.WriteLine(myEncoding.GetString(myByteArray));
// Increment the file pointer from CurrentPosition by one character.
myFileStream1.Seek(1,SeekOrigin.Current);
// Read the next five characeters.
myFileStream1.Read(myByteArray,0,5);
Console.WriteLine(myEncoding.GetString(myByteArray));
// Close the 'FileStream'.
myFileStream1.Close();
}
}
[C++]
// System::IO::SeekOrigin::Current
/*
The following example demonstrates the 'Current' member of 'SeekOrigin' enum. It creates a
'FileStream' Object with the file access option as writer. It creates a 'StreamWriter' Object
to write the data into the file. After writing the data successfully it reads the
first five characeters from the file. It increments the file pointer to one character
by using the filestream seek method. After that it displays the next five characters.
*/
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main() {
// Create a 'FileStream' Object.
FileStream* myFileStream = new FileStream(S"Names.txt", FileMode::OpenOrCreate, FileAccess::Write);
// Create a 'StreamWriter' to write the data into the file.
StreamWriter* myStreamWriter = new StreamWriter(myFileStream);
myStreamWriter->WriteLine(S"James Harry Robart");
// Update the 'StreamWriter'.
myStreamWriter->Flush();
// Close the 'StreamWriter' and FileStream.
myStreamWriter->Close();
myFileStream->Close();
FileStream* myFileStream1 = new FileStream(S"Names.txt", FileMode::OpenOrCreate, FileAccess::Read);
// Set the file pointer to the begin.
myFileStream1->Seek(0, SeekOrigin::Begin);
Byte myByteArray[] = new Byte[5];
// Read the first five characeters.
myFileStream1->Read(myByteArray,0,5);
ASCIIEncoding* myEncoding = new ASCIIEncoding();
Console::WriteLine(S"The Contents of the file are :");
Console::WriteLine(myEncoding->GetString(myByteArray));
// Increment the file pointer from CurrentPosition by one character.
myFileStream1->Seek(1,SeekOrigin::Current);
// Read the next five characeters.
myFileStream1->Read(myByteArray,0,5);
Console::WriteLine(myEncoding->GetString(myByteArray));
// Close the 'FileStream'.
myFileStream1->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.