Stream.CanRead プロパティ


派生クラスでオーバーライドされた場合は、現在のストリームが読み取りをサポートするかどうかを示す値を取得します。When overridden in a derived class, gets a value indicating whether the current stream supports reading.

 abstract property bool CanRead { bool get(); };
public abstract bool CanRead { get; }
member this.CanRead : bool
Public MustOverride ReadOnly Property CanRead As Boolean


ストリームが読み込みをサポートしている場合は true。それ以外の場合は falsetrue if the stream supports reading; otherwise, false.

CanRead プロパティの使用例を次に示します。The following is an example of using the CanRead property.

using namespace System;
using namespace System::IO;
int main( void )
   FileStream^ fs = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate,FileAccess::Read );
   if ( fs->CanRead && fs->CanWrite )
      Console::WriteLine( "MyFile.txt can be both written to and read from." );
      Console::WriteLine( "MyFile.txt is not writable" );

   return 0;

using System;
using System.IO;
class TestRW 
    public static void Main(String[] args)
        FileStream fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read);
        if (fs.CanRead && fs.CanWrite)
            Console.WriteLine("MyFile.txt can be both written to and read from.");
        else if (fs.CanRead)
            Console.WriteLine("MyFile.txt is not writable.");
Imports System.IO

Class TestRW

    Public Shared Sub Main()
        Dim fs As New FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read)
        If fs.CanRead And fs.CanWrite Then
            Console.WriteLine("MyFile.txt can be both written to and read from.")
            If fs.CanRead Then
                Console.WriteLine("MyFile.txt is not writable.")
            End If
        End If
    End Sub
End Class


Stream から派生したクラスが読み取りをサポートしていない場合、ReadReadByte、および BeginRead の各メソッドの呼び出しでは、NotSupportedExceptionがスローされます。If a class derived from Stream does not support reading, calls to the Read, ReadByte, and BeginRead methods throw a NotSupportedException.

ストリームが閉じている場合、このプロパティは falseを返します。If the stream is closed, this property returns false.