Stream.CanWrite 属性

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

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public MustOverride ReadOnly Property CanWrite As Boolean
用法
Dim instance As Stream
Dim value As Boolean

value = instance.CanWrite
public abstract bool CanWrite { get; }
public:
virtual property bool CanWrite {
    bool get () abstract;
}
/** @property */
public abstract boolean get_CanWrite ()
public abstract function get CanWrite () : boolean

属性值

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

备注

有关创建文件和向文件中写入文本的示例,请参见 如何:向文件写入文本。有关从文件中读取文本的示例,请参见 如何:从文件读取文本。有关读取和写入二进制文件的示例,请参见 如何:对新建的数据文件进行读取和写入

如果从 Stream 派生的类不支持写入,则调用 WriteBeginWriteWriteByte 将引发 NotSupportedException

如果该流已关闭,此属性将返回 false

示例

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

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.
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.
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.
import System.*;
import System.IO.*;

class TestRW
{
    public static void main(String[] args)
    {
        FileStream fs = new FileStream("MyFile.txt", 
            FileMode.OpenOrCreate, FileAccess.Write);
        if (fs.get_CanRead() && fs.get_CanWrite()) {
            Console.WriteLine(
                "MyFile.txt can be both written to and read from.");
        }
        else {
            if (fs.get_CanWrite()) {
                Console.WriteLine("MyFile.txt is writable.");
            }
        }
    } //main
} //TestRW

//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.

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Stream 类
Stream 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本