Stream.CanWrite 属性

定义

当在派生类中重写时,获取指示当前流是否支持写入功能的值。

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

属性值

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 因为它可以刷新只读流。

适用于

另请参阅