Windows apps
Collapse the table of content
Expand the table of content
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.CanSeek Property

Gets a value indicating whether the current stream supports seeking.

[Visual Basic]
Overrides Public ReadOnly Property CanSeek As Boolean
[C#]
public override bool CanSeek {get;}
[C++]
public: __property bool get_CanSeek();
[JScript]
public override function get CanSeek() : Boolean;

Property Value

true if the stream supports seeking; false if the stream is closed or if the FileStream was constructed from an operating-system handle such as a pipe or output to the console.

Remarks

If a class derived from Stream does not support seeking, calls to Length, SetLength, Position, and Seek throw a NotSupportedException.

If the stream is closed, this property returns false.

Example

[Visual Basic, C#, C++] The following example uses the CanSeek property to check whether a stream supports seeking.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Delete the file if it exists.
        If File.Exists(path) Then
            File.Delete(path)
        End If

        'Create the file.
        Dim fs As FileStream = File.Create(path)

        If fs.CanSeek Then
            Console.WriteLine("The stream connected to {0} is seekable.", path)
        Else
            Console.WriteLine("The stream connected to {0} is not seekable.", path)
        End If

        fs.Close()
    End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Text;

class Test 
{
    
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (File.Exists(path)) 
        {
            File.Delete(path);
        }

        //Create the file.
        using (FileStream fs = File.Create(path)) 
        {
            if (fs.CanSeek) 
            {
                Console.WriteLine("The stream connected to {0} is seekable.", path);
            } 
            else 
            {
                Console.WriteLine("The stream connected to {0} is not seekable.", path);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    // Delete the file if it exists.
    if (File::Exists(path)) {
        File::Delete(path);
    }

    //Create the file.
    FileStream* fs = File::Create(path);
    try {
        if (fs->CanSeek) {
            Console::WriteLine(S"The stream connected to {0} is seekable.", path);
        } else {
            Console::WriteLine(S"The stream connected to {0} is not seekable.", path);
        }
    } __finally {
        if (fs) __try_cast<IDisposable*>(fs)->Dispose();
    }
}

[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

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

Show:
© 2018 Microsoft