NetworkStream.CanRead Propriété

Définition

Obtient une valeur indiquant si NetworkStream prend en charge la lecture.Gets a value that indicates whether the NetworkStream supports reading.

public:
 virtual property bool CanRead { bool get(); };
public override bool CanRead { get; }
member this.CanRead : bool
Public Overrides ReadOnly Property CanRead As Boolean

Valeur de propriété

Boolean

true si les données du flux peuvent être lues ; sinon, false.true if data can be read from the stream; otherwise, false. La valeur par défaut est true.The default value is true.

Exemples

L’exemple de code suivant vérifie CanRead si le NetworkStream est accessible en lecture.The following code example checks CanRead to verify that the NetworkStream is readable. Il effectue ensuite une opération de lecture sur le NetworkStream .It then performs a read operation on the NetworkStream.

// Examples for CanRead, Read, and DataAvailable.
// Check to see if this NetworkStream is readable.
if ( myNetworkStream->CanRead )
{
   array<Byte>^ myReadBuffer = gcnew array<Byte>(1024);
   String^ myCompleteMessage = "";
   int numberOfBytesRead = 0;
   
   // Incoming message may be larger than the buffer size.
   do
   {
      numberOfBytesRead = myNetworkStream->Read( myReadBuffer, 0,
         myReadBuffer->Length );
      myCompleteMessage = String::Concat( myCompleteMessage,
         Encoding::ASCII->GetString( myReadBuffer, 0, numberOfBytesRead ) );
   }
   while ( myNetworkStream->DataAvailable );
   
   // Print out the received message to the console.
   Console::WriteLine( "You received the following message : {0}",
      myCompleteMessage );
}
else
{
   Console::WriteLine( "Sorry.  You cannot read from this NetworkStream." );
}
// Examples for CanRead, Read, and DataAvailable.

// Check to see if this NetworkStream is readable.
if(myNetworkStream.CanRead){
    byte[] myReadBuffer = new byte[1024];
    StringBuilder myCompleteMessage = new StringBuilder();
    int numberOfBytesRead = 0;

    // Incoming message may be larger than the buffer size.
    do{
         numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

         myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    }
    while(myNetworkStream.DataAvailable);

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                 myCompleteMessage);
}
else{
     Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
}

' Examples for CanRead, Read, and DataAvailable.
' Check to see if this NetworkStream is readable.
If myNetworkStream.CanRead Then
   Dim myReadBuffer(1024) As Byte
       Dim myCompleteMessage As StringBuilder = New StringBuilder()
   Dim numberOfBytesRead As Integer = 0
   
   ' Incoming message may be larger than the buffer size.
   Do
      numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
           myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
   Loop While myNetworkStream.DataAvailable
   
   ' Print out the received message to the console.
       Console.WriteLine(("You received the following message : " + myCompleteMessage.ToString()))
Else
   Console.WriteLine("Sorry.  You cannot read from this NetworkStream.")
End If

Remarques

Si CanRead a true la valeur, NetworkStream autorise les appels à la Read méthode.If CanRead is true, NetworkStream allows calls to the Read method. Fournissez la FileAccess valeur énumérée appropriée dans le constructeur pour définir la lisibilité et l’accessibilité de NetworkStream .Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability of the NetworkStream. La CanRead propriété est définie lorsque NetworkStream est initialisé.The CanRead property is set when the NetworkStream is initialized.

S’applique à

Voir aussi