System.Net.Sockets.Socket.SendFile Method

Sends the file fileName to a connected System.Net.Sockets.Socket object with the TransmitFileOptions.UseDefaultWorkerThread transmit flag.

Syntax

public void SendFile (string fileName)

Parameters

fileName
A string that contains the path and name of the file to be sent. This parameter can be null.

Remarks

This overload sends the file fileName to the connected socket. The flags parameter defaults to TransmitFileOptions.UseDefaultWorkerThread (0), and the preBuffer and postBuffer parameters default to null. If fileName is in the local directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. Wildcards ("..\\myfile.txt") and UNC share names ("\\\\shared directory\\myfile.txt") are supported. If the file is not found, the exception System.IO.FileNotFoundException is thrown.

This method uses the TransmitFile function found in the Windows Sockets 2 API. For more information about the TransmitFile function and its flags, see the Windows Sockets documentation in the MSDN Library.

Socket.SendFile(string) synchronously sends a file to the remote host specified in the Socket.Connect(System.Net.EndPoint) or Socket.Accept method. Socket.SendFile(string) can be used for both connection-oriented and for connectionless protocols.

If you are using a connectionless protocol, you must call Socket.Connect(System.Net.EndPoint) before calling this method, otherwise Socket.SendFile(string) throws a System.Net.Sockets.SocketException exception. If you are using a connection-oriented protocol, you must either use Socket.Connect(System.Net.EndPoint) to establish a remote host connection or use Socket.Accept to accept an incoming connection.

If you are using a connection-oriented protocol, Socket.SendFile(string) blocks until the file is sent. In nonblocking mode, Socket.SendFile(string) may complete successfully before the entire file has been sent. There is no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Socket.SendFile(string) method means that the underlying system has had room to buffer your data for a network send.

Note:

If you receive a System.Net.Sockets.SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0