Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

SeekOrigin Enumeration

Provides the fields that represent reference points in streams for seeking.

[Visual Basic]
<Serializable>
Public Enum SeekOrigin
[C#]
[Serializable]
public enum SeekOrigin
[C++]
[Serializable]
__value public enum SeekOrigin
[JScript]
public
   Serializable
enum SeekOrigin

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

SeekOrigin is used by the Seek methods of Stream, BufferedStream, FileStream, MemoryStream, BinaryWriter, and other classes. The Seek methods take an offset parameter that is relative to the position specified by SeekOrigin.

Members

Member name Description
Begin

Supported by the .NET Compact Framework.

Specifies the beginning of a stream.
Current

Supported by the .NET Compact Framework.

Specifies the current position within a stream.
End

Supported by the .NET Compact Framework.

Specifies the end of a stream.

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 Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.