FileStream.CanRead プロパティ

定義

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

public:
 virtual property bool CanRead { bool get(); };
public override bool CanRead { get; }
member this.CanRead : bool
Public Overrides ReadOnly Property CanRead As Boolean

プロパティ値

ストリームが読み取りをサポートしている場合は true。ストリームが閉じているか、書き込み専用アクセスで開かれた場合は false

次の例では、 プロパティの使用方法を CanRead 示します。 このコードの出力は、"MyFile.txt書き込み可能ではありません" です。"MyFile.txtの書き込みと読み取りの両方が可能です"という出力メッセージを取得するには、コンストラクターで FileStream パラメーターを FileAccessReadWrite変更します。

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." );
   }
   else
   {
      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.");
        }
    }
}
open System.IO

let fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read)

if fs.CanRead && fs.CanWrite then
    printfn "MyFile.txt can be both written to and read from."
else if fs.CanRead then
    printfn "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.")
        Else
            If fs.CanRead Then
                Console.WriteLine("MyFile.txt is not writable.")
            End If
        End If
    End Sub
End Class

注釈

からStream派生したクラスが読み取りをサポートしていない場合、 メソッドのReadByteRead呼び出しBeginReadは をNotSupportedExceptionスローします。

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

適用対象

こちらもご覧ください