FileStream.CanSeek Propriété

Définition

Obtient une valeur qui indique si le flux en cours prend en charge la recherche.

public:
 virtual property bool CanSeek { bool get(); };
public override bool CanSeek { get; }
member this.CanSeek : bool
Public Overrides ReadOnly Property CanSeek As Boolean

Valeur de propriété

Boolean

true si le flux prend en charge la recherche ; false si le flux est fermé ou si l'élément FileStream a été construit à partir d'un handle de système d'exploitation comme un canal ou une sortie console.

Exemples

L’exemple suivant utilise la CanSeek propriété pour vérifier si un flux prend en charge la recherche.

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

int main()
{
   String^ path = "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( "The stream connected to {0} is seekable.", path );
      }
      else
      {
         Console::WriteLine( "The stream connected to {0} is not seekable.", path );
      }
   }
   finally
   {
      if ( fs )
         delete (IDisposable^)fs;
   }
}
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);
            }
        }
    }
}
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

Remarques

Si une classe dérivée de Stream ne prend pas en charge la recherche, les appels à Length ,, SetLength Position et Seek lèvent NotSupportedException .

Si le flux est fermé, cette propriété retourne false .

S’applique à

Voir aussi