/// <summary>
        /// Download one file from remote to local
        /// </summary>
        /// <param name="request"></param>
        public void Download(FileTransferRequest request)
        {
            try
            {
                // Create download directory if not already exist
                var downloadDirectory = Path.GetDirectoryName(request.LocalFile);
                if (!Directory.Exists(downloadDirectory))
                {
                    Directory.CreateDirectory(downloadDirectory);
                }

                // download the file
                var credentials = new NetworkCredential(_userId, _password);
                using (var ftpClient = new FtpClient {
                    UsePassiveMode = _usePassive, Credentials = credentials
                })
                {
                    ftpClient.DownloadFile(request.RemoteFile, request.LocalFile);
                }
            }
            catch (Exception e)
            {
                //TODO (cr Oct 2009): we're not supposed to use SR for exception messages.
                //Throw a different type of exception and use an ExceptionHandler if it's supposed to be a user message.
                var message = string.Format(SR.ExceptionFailedToTransferFile, request.RemoteFile, request.LocalFile);
                throw new Exception(message, e);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Download one file from remote to local
        /// </summary>
        /// <param name="request"></param>
        public void Download(FileTransferRequest request)
        {
            try
            {
                using (var webClient = new WebClient())
                {
                    if (!string.IsNullOrEmpty(_userId) && !string.IsNullOrEmpty(_password))
                    {
                        webClient.Credentials = new NetworkCredential(_userId, _password);
                    }

                    var downloadDirectory = Path.GetDirectoryName(request.LocalFile);
                    if (!Directory.Exists(downloadDirectory))
                    {
                        Directory.CreateDirectory(downloadDirectory);
                    }

                    webClient.DownloadFile(request.RemoteFile, request.LocalFile);
                }
            }
            catch (Exception e)
            {
                //TODO (cr Oct 2009): we're not supposed to use SR for exception messages.
                //Throw a different type of exception and use an ExceptionHandler if it's supposed to be a user message.
                var message = string.Format(SR.ExceptionFailedToTransferFile, request.RemoteFile, request.LocalFile);
                throw new Exception(message, e);
            }
        }
        /// <summary>
        /// Upload one file from local to remote.
        /// </summary>
        /// <param name="request"></param>
        public void Upload(FileTransferRequest request)
        {
            try
            {
                CreateRemoteDirectoryForFile(request.RemoteFile);

                // upload the file
                var credentials = new NetworkCredential(_userId, _password);
                using (var ftpClient = new FtpClient {
                    UsePassiveMode = _usePassive, Credentials = credentials
                })
                {
                    ftpClient.UploadFile(request.RemoteFile, request.LocalFile);
                }
            }
            catch (Exception e)
            {
                //TODO (cr Oct 2009): we're not supposed to use SR for exception messages.
                //Throw a different type of exception and use an ExceptionHandler if it's supposed to be a user message.
                var message = string.Format(SR.ExceptionFailedToTransferFile, request.LocalFile, request.RemoteFile);
                throw new Exception(message, e);
            }
        }