Stream.CanWrite プロパティ

定義

派生クラスでオーバーライドされた場合は、現在のストリームが書き込みをサポートするかどうかを示す値を取得します。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.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 を呼び出すと、NotSupportedExceptionがスローされます。If 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.

ストリームが閉じている場合、このプロパティは falseを返します。If the stream is closed, this property returns false.

適用対象

こちらもご覧ください