This topic has not yet been rated - Rate this topic

NetworkStream.CanWrite Property

Gets a value that indicates whether the NetworkStream supports writing.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public override bool CanWrite { get; }

Property Value

Type: System.Boolean
true if data can be written to the NetworkStream; otherwise, false. The default value is true.

If CanWrite is true, NetworkStream allows calls to the Write method. Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability of the NetworkStream. The CanWrite property is set when the NetworkStream is initialized.

The following code example checks CanWrite to verify that the NetworkStream is writable. It then performs a write operation on the NetworkStream.


            // Examples for CanWrite, and CanWrite  

            // Check to see if this NetworkStream is writable.
            if (myNetworkStream.CanWrite){

                 byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
                 myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
            }
            else{
                 Console.WriteLine("Sorry.  You cannot write to this NetworkStream.");  
            }



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ