FileStream.CanSeek 속성

정의

현재 스트림이 검색을 지원하는지 여부를 나타내는 값을 가져옵니다.

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

속성 값

스트림이 검색을 지원하면 true이고, 스트림이 닫혔거나 FileStream이 콘솔에 대한 출력 또는 파이프와 같은 운영 체제 핸들로부터 생성된 경우에는 false입니다.

예제

다음 예제에서는 사용 하 CanSeek 여는 속성을 스트림에서 검색을 지원 하는지 여부를 검사 합니다.

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);
            }
        }
    }
}
open System.IO

let path = @"c:\temp\MyTest.txt"

// Delete the file if it exists.
if File.Exists path then
    File.Delete path


//Create the file.
do
    use fs = File.Create path

    if fs.CanSeek then
        printfn $"The stream connected to {path} is seekable."
    else
        printfn $"The stream connected to {path} is not seekable."
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

설명

에서 Stream 파생된 클래스가 검색을 지원하지 않는 경우 , , SetLengthPositionLength호출하고 Seek 을 throw합니다NotSupportedException.

스트림이 닫힌 경우 이 속성은 를 반환합니다 false.

적용 대상

추가 정보