Socket.SendFile Method (String, Byte, Byte, TransmitFileOptions)
Assembly: System (in System.dll)
member SendFile : fileName:string * preBuffer:byte * postBuffer:byte * flags:TransmitFileOptions -> unit
A String that contains the path and name of the file to be sent. This parameter can be null.
A Byte array that contains data to be sent before the file is sent. This parameter can be null.
A Byte array that contains data to be sent after the file is sent. This parameter can be null.
The operating system is not Windows NT or later.
- or -
The socket is not connected to a remote host.
The Socket object has been closed.
The Socket object is not in blocking mode and cannot accept this synchronous call.
The file fileName was not found.
An error occurred when attempting to access the socket. See the Remarks section for more information.
This overload requires the name of the file you want to send and a bitwise combination of TransmitFileOptions values. The preBuffer parameter contains any data you want to precede the file. postBuffer contains data you want to follow the file. If fileName is in the current working 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.
The flags parameter provides the Window Sockets service provider with additional information about the file transfer. For more information about how to use this parameter, see TransmitFileOptions.
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.
If you are using a connectionless protocol, you must call Connect before calling this method; otherwise SendFile throws a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.
If you are using a connection-oriented protocol, SendFile blocks until the entire file is sent. In nonblocking mode, SendFile 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 SendFile method means that the underlying system has had room to buffer your data for a network send.
If you receive a 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.
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
The following code example creates and connects a socket. The file "test.txt" is located in the root directory of the local machine. In this example, we create a prebuffer and postbuffer of data and send them to the remote host with the file. The default TransmitFileOptions are used.
Available since 2.0