Stream.CanWrite 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,取得指示目前資料流是否支援寫入的數值。
public:
abstract property bool CanWrite { bool get(); };
public abstract bool CanWrite { get; }
member this.CanWrite : bool
Public MustOverride ReadOnly Property CanWrite As Boolean
屬性值
如果資料流支援寫入,則為 true
;否則,為 false
。
範例
以下是使用 屬性的 CanWrite
範例。
using namespace System;
using namespace System::IO;
int main()
{
FileStream^ fs = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate,FileAccess::Write );
if ( fs->CanRead && fs->CanWrite )
{
Console::WriteLine( "MyFile.txt can be both written to and read from." );
}
else
if ( fs->CanWrite )
{
Console::WriteLine( "MyFile.txt is writable." );
}
}
//This code outputs "MyFile.txt is writable."
//To get the output message "MyFile.txt can be both written to and read from.",
//change the FileAccess parameter to ReadWrite in the FileStream constructor.
using System;
using System.IO;
class TestRW
{
public static void Main(String[] args)
{
FileStream fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate,
FileAccess.Write);
if (fs.CanRead && fs.CanWrite) {
Console.WriteLine("MyFile.txt can be both written to and read from.");
}
else if (fs.CanWrite) {
Console.WriteLine("MyFile.txt is writable.");
}
}
}
//This code outputs "MyFile.txt is writable."
//To get the output message "MyFile.txt can be both written to and read from.",
//change the FileAccess parameter to ReadWrite in the FileStream constructor.
Imports System.IO
Class TestRW
Public Shared Sub Main()
Dim fs As New FileStream("MyFile.txt", FileMode.OpenOrCreate, _
FileAccess.Write)
If fs.CanRead And fs.CanWrite Then
Console.WriteLine("MyFile.txt can be both written to and read from.")
Else
If fs.CanWrite Then
Console.WriteLine("MyFile.txt is writable.")
End If
End If
End Sub
End Class
'This code outputs "MyFile.txt is writable."
'To get the output message "MyFile.txt can be both written to and read from.",
'change the FileAccess parameter to ReadWrite in the FileStream constructor.
備註
如果衍生自 Stream 的類別不支援寫入, 則呼叫 Write、 BeginWrite或 WriteByte 會 NotSupportedException擲回 。 在這種情況下, Flush 通常會實作為空的方法,以確保與其他 Stream 類型完全相容,因為排清只讀數據流是有效的。