語言

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 可以同時寫入或讀取。」,請在建構子中將參數改FileAccess為 。ReadWriteFileStream

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 的類別不支援讀取,呼叫 Read、 ReadByte及 BeginRead 方法會拋出 NotSupportedException。

若串流封閉,此性質返回 false。

適用於

另請參閱