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

属性值

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);
            }
        }
    }
}
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 不支持寻找,则调用 Length SetLength Position 、、 和 Seek ,并引发 NotSupportedException

如果流已关闭,则此属性返回 false

适用于

另请参阅