次の方法で共有


FileStream.CanSeek プロパティ

現在のストリームがシークをサポートしているかどうかを示す値を取得します。

Overrides Public ReadOnly Property CanSeek As Boolean
[C#]
public override bool CanSeek {get;}
[C++]
public: __property bool get_CanSeek();
[JScript]
public override function get CanSeek() : Boolean;

プロパティ値

ストリームがシークをサポートしている場合は true 。ストリームが閉じているか、 FileStream がパイプまたはコンソールへの出力などのオペレーティング システム ハンドルから構築された場合は false

解説

Stream から派生したクラスがシークをサポートしていない場合に、 LengthSetLengthPosition 、および Seek を呼び出すと、 NotSupportedException がスローされます。

ストリームが閉じている場合、このプロパティは false を返します。

使用例

[Visual Basic, C#, C++] 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

[C#] 
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);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

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

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

FileStream クラス | FileStream メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み