FileStream.CanWrite Property
.NET Framework (current version)
Gets a value indicating whether the current stream supports writing.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Booleantrue if the stream supports writing; false if the stream is closed or was opened with read-only access.
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.
The following example uses the CanWrite property to check whether a stream supports writing.
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
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.
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
Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: