FileStream.CanSeek 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,表示目前資料流是否支援搜尋。
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 類別不支援搜尋,則呼叫Length、 SetLengthPosition和 Seek 會擲回 NotSupportedException。
如果資料流已關閉,這個屬性會傳 false
回 。