Stream.CanWrite Stream.CanWrite Stream.CanWrite Stream.CanWrite Property

定義

在衍生類別中覆寫時,取得指示目前資料流是否支援寫入的數值。When overridden in a derived class, gets a value indicating whether the current stream supports writing.

public:
 abstract property bool CanWrite { bool get(); };
public abstract bool CanWrite { get; }
member this.CanWrite : bool
Public MustOverride ReadOnly Property CanWrite As Boolean

屬性值

如果資料流支援寫入,則為 true;否則,為 falsetrue if the stream supports writing; otherwise, false.

範例

以下是使用的範例CanWrite屬性。The following is an example of using the CanWrite property.

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
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不支援寫入,呼叫WriteBeginWrite,或WriteByte就會擲回NotSupportedExceptionIf a class derived from Stream does not support writing, a call to Write, BeginWrite, or WriteByte throws a NotSupportedException. 在此情況下,Flush通常會實作為空的方法,以確保完整的相容性與其他Stream類型,因為它是有效排清的唯讀資料流。In such cases, Flush is typically implemented as an empty method to ensure full compatibility with other Stream types since it's valid to flush a read-only stream.

如果資料流已關閉,則這個屬性會傳回falseIf the stream is closed, this property returns false.

適用於

另請參閱