Stream.CanWrite 属性

定义

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

C#
public abstract bool CanWrite { get; }

属性值

如果流支持写入,则为 true;否则为 false

示例

下面是使用 CanWrite 属性的示例。

C#
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.

注解

如果派生自 Stream 的类不支持写入,则对 WriteBeginWriteWriteByte 的调用将 NotSupportedException引发 。 在这种情况下, Flush 通常实现为空方法,以确保与其他 Stream 类型完全兼容,因为刷新只读流是有效的。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅