FileStream.CanSeek 属性

获取一个值,该值指示当前流是否支持查找。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides ReadOnly Property CanSeek As Boolean
用法
Dim instance As FileStream
Dim value As Boolean

value = instance.CanSeek
public override bool CanSeek { get; }
public:
virtual property bool CanSeek {
    bool get () override;
}
/** @property */
public boolean get_CanSeek ()
public override function get CanSeek () : boolean

属性值

如果流支持查找,则为 true;如果流已关闭或者如果 FileStream 是从操作系统句柄(如管道或到控制台的输出)构造的,则为 false

备注

如果从 Stream 派生的类不支持查找,则对 LengthSetLengthPositionSeek 的调用将引发 NotSupportedException

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

示例

下面的示例使用 CanSeek 属性检查流是否支持查找。

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
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);
            }
        }
    }
}
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;
   }
}
import System.*;
import System.IO.*;
import System.Text.*;

class Test
{
    public static void main(String[] args)
    {
        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.get_CanSeek()) {
                Console.WriteLine("The stream connected to {0} " 
                    + "is seekable.", path);
            }
            else {
                Console.WriteLine("The stream connected to {0} is not " 
                    + " seekable.", path);
            }
        }
        finally {
            fs.Dispose();
        }

    } //main
} //Test

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FileStream 类
FileStream 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本