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 쓰고 읽을 수 있습니다."라는 출력 메시지를 얻으려면 매개 변수를 FileAccessReadWrite 생성자에서 FileStream 변경합니다.

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 클래스가 읽기를 지원하지 않는 경우 , ReadReadByte 메서드에 BeginRead대한 호출은 을 throw합니다NotSupportedException.

스트림이 닫혀 있으면 이 속성이 반환됩니다 false.

적용 대상

추가 정보