System.IO.SeekOrigin Enumeration

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Provides the fields that represent reference points in streams for seeking.
C# Syntax:
[Serializable]
public enum SeekOrigin
Remarks
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.
Example
This example shows a use of SeekOrigin with StreamReader.BaseStream and Stream.Seek to set the file pointer of the underlying stream to the beginning.
 FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
    FileAccess.Read);
 // Create a Char reader.
 StreamReader w = new StreamReader(fs);
 // Set the StreamReader file pointer to the end.
 w.BaseStream.Seek(0, SeekOrigin.End);

    
// 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();

      }
}

    
See also:
System.IO Namespace

System.IO.SeekOrigin Member List:

Public Fields
Begin Specifies the beginning of a stream.
Current Specifies the current position within a stream.
End Specifies the end of a stream.

Hierarchy:


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.