Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Stream.Seek Method

When overridden in a derived class, sets the position within the current stream.

[Visual Basic]
Public MustOverride Function Seek( _
   ByVal offset As Long, _
   ByVal origin As SeekOrigin _
) As Long
[C#]
public abstract long Seek(
 long offset,
 SeekOrigin origin
);
[C++]
public: virtual __int64 Seek(
 __int64 offset,
 SeekOrigin origin
) = 0;
[JScript]
public abstract function Seek(
   offset : long,
 origin : SeekOrigin
) : long;

Parameters

offset
A byte offset relative to the origin parameter.
origin
A value of type SeekOrigin indicating the reference point used to obtain the new position.

Return Value

The new position within the current stream.

Exceptions

Exception Type Condition
IOException An I/O error occurs.
NotSupportedException The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
ObjectDisposedException Methods were called after the stream was closed.

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.

Use the CanSeek property to determine whether the current instance supports seeking.

If offset is negative, the new position is required to precede the position specified by origin by the number of bytes specified by offset. If offset is zero (0), the new position is required to be the position specified by origin. If offset is positive, the new position is required to follow the position specified by origin by the number of bytes specified by offset.

Classes derived from Stream that support seeking must override this method to provide the functionality described above.

Seeking to any location beyond the length of the stream is supported.

Example

[Visual Basic, C#, C++] The following example shows a use of SeekOrigin with BaseStream and Seek to set the file pointer of the underlying stream to the beginning. Note that the file log.txt is created in the current directory unless otherwise specified.

[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);
}

[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

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, Common Language Infrastructure (CLI) Standard

See Also

Stream Class | Stream Members | 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.