Stream.CanRead 属性

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

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

语法

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

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

属性值

如果流支持读取,为 true;否则为 false

备注

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

如果从 Stream 派生的类不支持读取,则调用 ReadReadByteBeginRead 方法将引发 NotSupportedException

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

示例

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

Imports System
Imports System.IO

Class TestRW

    Public Shared Sub Main()
        Dim fs As New FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read)
        If fs.CanRead And fs.CanWrite Then
            Console.WriteLine("MyFile.txt can be both written to and read from.")
        Else
            If fs.CanRead Then
                Console.WriteLine("MyFile.txt is not writable.")
            End If
        End If
    End Sub
End Class
using System;
using System.IO;
 
class TestRW 
{
    public static void Main(String[] args)
    {
        FileStream fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read);
        if (fs.CanRead && fs.CanWrite)
        {
            Console.WriteLine("MyFile.txt can be both written to and read from.");
        }
        else if (fs.CanRead)
        {
            Console.WriteLine("MyFile.txt is not writable.");
        }
    }
}
using namespace System;
using namespace System::IO;
int main( void )
{
   FileStream^ fs = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate,FileAccess::Read );
   if ( fs->CanRead && fs->CanWrite )
   {
      Console::WriteLine( "MyFile.txt can be both written to and read from." );
   }
   else
   {
      Console::WriteLine( "MyFile.txt is not writable" );
   }

   return 0;
}
import System.*;
import System.IO.*;

class TestRW
{
    public static void main(String[] args)
    {
        FileStream fs = new FileStream("MyFile.txt", 
            FileMode.OpenOrCreate, FileAccess.Read);
        if (fs.get_CanRead() && fs.get_CanWrite()) {
            Console.WriteLine("MyFile.txt can be both written to " 
                + "and read from.");
        }
        else {
            if (fs.get_CanRead()) {
                Console.WriteLine("MyFile.txt is not writable.");
            }
        }
    } //main
} //TestRW
 import System;
 import System.IO;
 
class TestRW 
{
   public static function Main() : void 
   {
 
         var fs : FileStream = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Read);
         if (fs.CanRead && fs.CanWrite)
         {
            Console.WriteLine("MyFile.txt can be both written to and read from.");
         }
         else if (fs.CanRead)
         {
            Console.WriteLine("MyFile.txt is not writable.");
      }
   }
}
TestRW.Main();

平台

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
如何:从文件读取文本
如何:向文件写入文本