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.

FileStream.CanWrite Property

Gets a value indicating whether the current stream supports writing.

[Visual Basic]
Overrides Public ReadOnly Property CanWrite As Boolean
[C#]
public override bool CanWrite {get;}
[C++]
public: __property bool get_CanWrite();
[JScript]
public override function get CanWrite() : Boolean;

Property Value

true if the stream supports writing; false if the stream is closed or was opened with read-only access.

Remarks

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

If the stream is closed, this property returns false.

Example

The following example uses the CanWrite property to check whether a stream supports writing.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        'Ensure that the file is readonly.
        File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.ReadOnly)

        'Create the file.
        Dim fs As FileStream = New FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)

        If fs.CanWrite Then
            Console.WriteLine("The stream connected to {0} is writable.", path)
        Else
            Console.WriteLine("The stream connected to {0} is not writable.", path)
        End If
        fs.Close()
    End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Text;

class Test 
{
    
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Ensure that the file is readonly.
        File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);

        //Create the file.
        using (FileStream fs = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) 
        {
            if (fs.CanWrite) 
            {
                Console.WriteLine("The stream for file {0} is writable.", path);
            } 
            else 
            {
                Console.WriteLine("The stream for file {0} is not writable.", path);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    // Ensure that the file is readonly.
    File::SetAttributes(path, static_cast<FileAttributes>(File::GetAttributes(path) | FileAttributes::ReadOnly));

    //Create the file.
    FileStream* fs = new FileStream (path, FileMode::OpenOrCreate, FileAccess::Read);
    try {
        if (fs->CanWrite) {
            Console::WriteLine(S"The stream for file {0} is writable.", path);
        } else {
            Console::WriteLine(S"The stream for file {0} is not writable.", path);
        }
    } __finally {
        if (fs) __try_cast<IDisposable*>(fs)->Dispose();
    }
}

The following is an example using the CanWrite property. The output of this code is "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.

[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.")
        ElseIf fs.CanWrite Then
            Console.WriteLine("MyFile.txt is writable.")
        End If
    End Sub
End Class

[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.");
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main(void)
    {
        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 
        {
        Console::WriteLine(S"MyFile.txt is writable.");
    }
    return 0;
}

[JScript] 
import System;
import System.IO;

class TestRW 
{
   public static function Main() : void 
   {
       var fs : FileStream = 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.");
    }
}
TestRW.Main();

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

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

Show:
© 2014 Microsoft