Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Stream.CanWrite Property

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

[Visual Basic]
Public MustOverride ReadOnly Property CanWrite As Boolean
[C#]
public abstract bool CanWrite {get;}
[C++]
public: __property virtual bool get_CanWrite() = 0;
[JScript]
public abstract function get CanWrite() : Boolean;

Property Value

true if the stream supports writing; otherwise, false.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

If a class derived from Stream does not support writing, a call to Write, BeginWrite, or WriteByte throws a NotSupportedException.

If the stream is closed, this property returns false.

Example

[Visual Basic, C#, C++] The following is an example of using the CanWrite property.

[Visual Basic] 
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.

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int main()
{ 
    FileStream* fs = new FileStream(S"MyFile.txt", FileMode::OpenOrCreate,
        FileAccess::Write);
    if (fs->CanRead && fs->CanWrite) {
        Console::WriteLine(S"MyFile.txt can be both written to and read from.");
    }
    else if (fs->CanWrite) {
        Console::WriteLine(S"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.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Stream Class | Stream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft