Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileStream.Seek Method

Updated: January 2010

Sets the current position of this stream to the given value.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public override long Seek(
	long offset,
	SeekOrigin origin
)

Parameters

offset
Type: System.Int64

The point relative to origin from which to begin seeking.

origin
Type: System.IO.SeekOrigin

Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type SeekOrigin.

Return Value

Type: System.Int64
The new position in the stream.

ExceptionCondition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking, such as if the FileStream is constructed from a pipe or console output.

ArgumentException

Attempted seeking before the beginning of the stream.

ObjectDisposedException

Methods were called after the stream was closed.

This method overrides Stream.Seek.

NoteNote:

Use the FileStream.CanSeek property to determine whether the current instance supports seeking. For additional information, see Stream.CanSeek.

You can seek to any location beyond the length of the stream. When you seek beyond the length of the file, the file size grows. In Windows NT and later versions, data added to the end of the file is set to zero. In Windows 98 or earlier versions, data added to the end of the file is not set to zero, which means that previously deleted data is visible to the stream.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.

using System;
using System.IO;

class FStream
{
    static void Main()
    {
        const string fileName = "Test#@@#.dat";

        // Create random data to write to the file.
        byte[] dataArray = new byte[100000];
        new Random().NextBytes(dataArray);

        using(FileStream  
            fileStream = new FileStream(fileName, FileMode.Create))
        {
            // Write the data to the file, byte by byte. 
            for(int i = 0; i < dataArray.Length; i++)
            {
                fileStream.WriteByte(dataArray[i]);
            }

            // Set the stream position to the beginning of the file.
            fileStream.Seek(0, SeekOrigin.Begin);

            // Read and verify the data. 
            for(int i = 0; i < fileStream.Length; i++)
            {
                if(dataArray[i] != fileStream.ReadByte())
                {
                    Console.WriteLine("Error writing data.");
                    return;
                }
            }
            Console.WriteLine("The data was written to {0} " +
                "and verified.", fileStream.Name);
        }
    }
}

The following example reads text from a file backwards by using the various SeekOrigin values with the FileStream.Seek method.

using System;
using System.IO;

public class FSSeek
{
    public static void Main()
    {
        // temp.txt contains "This is a test!"

        FileStream fs = new FileStream(@"c:\temp\temp.txt", FileMode.Open, FileAccess.Read);
        long offset;

        // read the file backwards using SeekOrigin.Begin... 
        for (offset = fs.Length - 1; offset >= 0; offset--)
        {
            fs.Seek(offset, SeekOrigin.Begin);
            Console.Write(Convert.ToChar(fs.ReadByte()));
        }
        Console.WriteLine();

        // read the file backwards using SeekOrigin.End... 
        for (offset = 1; offset <= fs.Length; offset++)
        {
            fs.Seek(-offset, SeekOrigin.End);
            Console.Write(Convert.ToChar(fs.ReadByte()));
        }
        Console.WriteLine();

        // read the file backwards using SeekOrigin.Current...
        fs.Seek(0, SeekOrigin.End);
        for (offset = 0; offset < fs.Length; offset++)
        {
            fs.Seek(-1, SeekOrigin.Current);
            Console.Write(Convert.ToChar(fs.ReadByte()));
            fs.Seek(-1, SeekOrigin.Current);
        }
        Console.WriteLine();
        fs.Close();
    }
}
// This code example displays the following output: 
// 
// !tset a si sihT 
// !tset a si sihT 
// !tset a si sihT

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

January 2010

Added second example.

Customer feedback.

Community Additions

Show:
© 2014 Microsoft