FileStream.CanRead 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,表示目前資料流是否支援讀取。
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
將 FileAccess
參數變更為 ReadWrite
。
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 的類別不支援讀取, 則呼叫、 ReadByte和 BeginRead 方法會Read擲回 NotSupportedException。
如果資料流已關閉,這個屬性會傳 false
回 。