Exemplo n.º 1
0
        public void FxpCopy(string fileName, FtpClient destination)
        {
            if (this.IsConnected == false)
            {
                throw new FtpException("The connection must be open before a transfer between servers can be initiated.");
            }

            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (destination.IsConnected == false)
            {
                throw new FtpException("The destination object must be open and connected before a transfer between servers can be initiated.");
            }

            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (fileName.Length == 0)
            {
                throw new ArgumentException("must have a value", "fileName");
            }

            try
            {
                destination.SendRequest(new FtpRequest(base.CharacterEncoding, FtpCmd.Pasv));
            }
            catch (FtpException fex)
            {
                throw new FtpException(String.Format("An error occurred when trying to set up the passive connection on '{1}' for a destination to destination copy between '{0}' and '{1}'.", this.Host, destination.Host), base.LastResponse, fex);
            }

            int startIdx = destination.LastResponse.Text.IndexOf("(") + 1;
            int endIdx = destination.LastResponse.Text.IndexOf(")");
            string dataPortInfo = destination.LastResponse.Text.Substring(startIdx, endIdx - startIdx);

            try
            {
                this.SendRequest(new FtpRequest(base.CharacterEncoding, FtpCmd.Port, dataPortInfo));
            }
            catch (FtpException fex)
            {
                throw new FtpException(String.Format("Command instructing '{0}' to open connection failed.", this.Host), base.LastResponse, fex);
            }

            try
            {
                this.SendRequest(new FtpRequest(base.CharacterEncoding, FtpCmd.Retr, fileName));
            }
            catch (FtpException fex)
            {
                throw new FtpException(String.Format("An error occurred transferring on a server to server copy between '{0}' and '{1}'.", this.Host, destination.Host), base.LastResponse, fex);
            }

            try
            {
                destination.SendRequest(new FtpRequest(base.CharacterEncoding, FtpCmd.Stor, fileName));
            }
            catch (FtpException fex)
            {
                throw new FtpException(String.Format("An error occurred transferring on a server to server copy between '{0}' and '{1}'.", this.Host, destination.Host), base.LastResponse, fex);
            }

            destination.WaitForHappyCodes(this.FxpTransferTimeout, FtpResponseCode.RequestedFileActionOkayAndCompleted, FtpResponseCode.ClosingDataConnection);
            this.WaitForHappyCodes(this.FxpTransferTimeout, FtpResponseCode.RequestedFileActionOkayAndCompleted, FtpResponseCode.ClosingDataConnection);
        }