FXP transfers are initiated with the static method FtpClient.FxpCopy(). Before the method is called, the context must be properly set up.
Step by step instructions
-
Because FtpClient.FxpCopy() is static, two independent FtpClient objects are needed. One client for the source file on the source server, another client for the destination file on the destination server.
// Create a FTP client to control our source server FtpClient sourceClient = new FtpClient(); // Create a FTP client to control our destination server FtpClient destinationClient = new FtpClient();
' Create a FTP client to control our source server Dim sourceClient As New FtpClient() ' Create a FTP client to control our destination server Dim destinationClient As New FtpClient()
-
Connect to each server using the needed credentials for each server.
// Connect to the source server sourceClient.Connect( "localhost", 21 ); sourceClient.Login( "normal", "normal" ); // Connect to the destination server destinationClient.Connect( "localhost", 23 ); destinationClient.Login( "normal", "normal" );
' Connect to the source server sourceClient.Connect("localhost", 21) sourceClient.Login("normal", "normal") ' Connect to the destination server destinationClient.Connect("localhost", 23) destinationClient.Login("normal", "normal")
-
Make sure the properties on each FtpClient are valid for FXP transfers.
/* For a FXP transfer, only one client can be setup for passive transfer. The FxpServer enumeration allows to select either the source or destination client as the one that will be setup as passive. A FxpServer value is supplied to FtpClient.FxpCopy() */ FxpServer passiveServer = FxpServer.Source; /* For a FXP transfer, both clients must use the same representation type. The default values suit us fine here. */ //sourceClient.RepresentationType = RepresentationType.Binary; //destinationClient.RepresentationType = RepresentationType.Binary; /* For a FXP transfer, both clients must use the same transfer mode. The default values suit us fine here. */ //sourceClient.TransferMode = TransferMode.Stream; //destinationClient.TransferMode = TransferMode.Stream;
' For a FXP transfer, only one client can be setup for passive transfer. ' The FxpServer enumeration allows to select either the source or destination client ' as the one that will be setup as passive. ' ' A FxpServer value is supplied to FtpClient.FxpCopy() Dim passiveServer As FxpServer = FxpServer.Source ' For a FXP transfer, both clients must use the same representation type. ' The default values suit us fine here. 'sourceClient.RepresentationType = RepresentationType.Binary; 'destinationClient.RepresentationType = RepresentationType.Binary; ' For a FXP transfer, both clients must use the same transfer mode. ' The default values suit us fine here. 'sourceClient.TransferMode = TransferMode.Stream; 'destinationClient.TransferMode = TransferMode.Stream;
-
Select source and destination files and call FtpClient.FxpCopy().
// Change the current folder on the source server to get to the file we want sourceClient.ChangeCurrentFolder( "normal1" ); // Copy a file from the source server's current folder to the destination server giving the file a specific name FtpClient.FxpCopy( sourceClient, "tvDebug.log", destinationClient, "FxpCopy.xml", passiveServer );
' Change the current folder on the source server to get to the file we want sourceClient.ChangeCurrentFolder("normal1") ' Copy a file from the source server's current folder to the destination server giving the file a specific name FtpClient.FxpCopy(sourceClient, "tvDebug.log", destinationClient, "FxpCopy.xml", passiveServer)
Putting it all together
Complete example
// Create a FTP client to control our source server FtpClient sourceClient = new FtpClient(); // Create a FTP client to control our destination server FtpClient destinationClient = new FtpClient(); try { // Connect to the source server sourceClient.Connect( "localhost", 21 ); /* FXP transfers cannot be performed over encrypted data channels. While it is ok to encrypt the control channel, the data channel must remain clear. The DataChannelProtection.Clear value ensures that. */ //sourceClient.Authenticate( AuthenticationMethod.Tls, VerificationFlags.None, null, DataChannelProtection.Clear ); // Login to the source server sourceClient.Login( "normal", "normal" ); // Connect to the destination server destinationClient.CertificateReceived += delegate( object sender, CertificateReceivedEventArgs e ) { e.Action = VerificationAction.Accept; }; destinationClient.Connect( "localhost", 10021 ); /* FXP transfers cannot be performed over encrypted data channels. While it is ok to encrypt the control channel, the data channel must remain clear. The DataChannelProtection.Clear value ensures that. */ //destinationClient.Authenticate( AuthenticationMethod.Ssl, VerificationFlags.None, null, DataChannelProtection.Clear ); // Login to the destination server destinationClient.Login( "normal", "normal" ); /* For a FXP transfer, only one client can be setup for passive transfer. The FxpServer enumeration allows to select either the source or destination client as the one that will be setup as passive. A FxpServer value is supplied to FtpClient.FxpCopy() */ FxpServer passiveServer = FxpServer.Source; // Change the current folder on the source server to get to the file we want sourceClient.ChangeCurrentFolder( "normal1" ); // Copy a file from the source server's current folder to the destination server giving the file a specific name FtpClient.FxpCopy( sourceClient, "tvDebug.log", destinationClient, "FxpCopy.xml", passiveServer ); // Copy a file from a specific path on the source server to the destination server using the same name FtpClient.FxpCopy( sourceClient, "/SftpFolder5/Folder2/ntprint.inf", destinationClient, "ntprint.inf", passiveServer ); // Copy a file from a specific path on the source server to a specific folder relative to the current folder on the destination server FtpClient.FxpCopy( sourceClient, "/SftpFolder5/Folder2/ntprint.inf", destinationClient, "Folder2/ntprint.inf", passiveServer ); } finally { if( sourceClient.Connected ) sourceClient.Disconnect(); if( destinationClient.Connected ) destinationClient.Disconnect(); }
' Create a FTP client to control our source server Dim sourceClient As New FtpClient() ' Create a FTP client to control our destination server Dim destinationClient As New FtpClient() Try ' Connect to the source server sourceClient.Connect("localhost", 21) ' FXP transfers cannot be performed over encrypted data channels. While it is ok ' to encrypt the control channel, the data channel must remain clear. The DataChannelProtection.Clear ' value ensures that. 'sourceClient.Authenticate( AuthenticationMethod.Tls, VerificationFlags.None, null, DataChannelProtection.Clear ); ' Login to the source server sourceClient.Login("normal", "normal") ' Connect to the destination server AddHandler destinationClient.CertificateReceived, AddressOf AnonymousMethod1 destinationClient.Connect("localhost", 10021) ' FXP transfers cannot be performed over encrypted data channels. While it is ok ' to encrypt the control channel, the data channel must remain clear. The DataChannelProtection.Clear ' value ensures that. 'destinationClient.Authenticate( AuthenticationMethod.Ssl, VerificationFlags.None, null, DataChannelProtection.Clear ); ' Login to the destination server destinationClient.Login("normal", "normal") ' For a FXP transfer, only one client can be setup for passive transfer. ' The FxpServer enumeration allows to select either the source or destination client ' as the one that will be setup as passive. ' ' A FxpServer value is supplied to FtpClient.FxpCopy() Dim passiveServer As FxpServer = FxpServer.Source ' Change the current folder on the source server to get to the file we want sourceClient.ChangeCurrentFolder("normal1") ' Copy a file from the source server's current folder to the destination server giving the file a specific name FtpClient.FxpCopy(sourceClient, "tvDebug.log", destinationClient, "FxpCopy.xml", passiveServer) ' Copy a file from a specific path on the source server to the destination server using the same name FtpClient.FxpCopy(sourceClient, "/SftpFolder5/Folder2/ntprint.inf", destinationClient, "ntprint.inf", passiveServer) ' Copy a file from a specific path on the source server to a specific folder relative to the current folder on the destination server FtpClient.FxpCopy(sourceClient, "/SftpFolder5/Folder2/ntprint.inf", destinationClient, "Folder2/ntprint.inf", passiveServer) Finally If sourceClient.Connected Then sourceClient.Disconnect() End If If destinationClient.Connected Then destinationClient.Disconnect() End If End Try