コード例 #1
0
        /// <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);
            }
        }
コード例 #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);
            }
        }
コード例 #3
0
        /// <summary>
        /// Download one file from remote to local
        /// </summary>
        /// <param name="request"></param>
        public void Download(FileTransferRequest request)
        {
            try
            {
                // Create a FTP request to download file
                var ftpRequest = (FtpWebRequest)WebRequest.Create(request.RemoteFile);
                ftpRequest.Method      = WebRequestMethods.Ftp.DownloadFile;
                ftpRequest.UseBinary   = true;
                ftpRequest.UsePassive  = _usePassive;
                ftpRequest.Credentials = new NetworkCredential(_userId, _password);

                // Create download directory if not already exist
                var downloadDirectory = Path.GetDirectoryName(request.LocalFile);
                if (!Directory.Exists(downloadDirectory))
                {
                    Directory.CreateDirectory(downloadDirectory);
                }

                // Open ftp and local streams

                using (var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
                    using (var ftpResponseStream = ftpResponse.GetResponseStream())
                        using (var localFileStream = new FileStream(request.LocalFile, FileMode.Create))
                        {
                            // Write Content from the FTP download stream to local file stream
                            const int bufferSize = 2048;
                            var       buffer     = new byte[bufferSize];
                            var       readCount  = ftpResponseStream.Read(buffer, 0, bufferSize);
                            while (readCount > 0)
                            {
                                localFileStream.Write(buffer, 0, readCount);
                                readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
                            }
                        }
            }
            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);
            }
        }
コード例 #4
0
		/// <summary>
		/// Upload one file from local to remote.
		/// </summary>
		/// <param name="request"></param>
		/// <remarks>
		/// The remote directories are not created before uploading files.
		/// </remarks>
		public void Upload(FileTransferRequest request)
		{
			try
			{
				using (var webClient = new WebClient())
				{
					if (!string.IsNullOrEmpty(_userId) && !string.IsNullOrEmpty(_password))
						webClient.Credentials = new NetworkCredential(_userId, _password);

					webClient.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.RemoteFile, request.LocalFile);
				throw new Exception(message, e);
			}
		}
コード例 #5
0
ファイル: FtpFileTransfer.cs プロジェクト: nhannd/Xian
		/// <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);
			}
		}
コード例 #6
0
        /// <summary>
        /// Upload one file from local to remote.
        /// </summary>
        /// <param name="request"></param>
        public void Upload(FileTransferRequest request)
        {
            try
            {
                CreateRemoteDirectoryForFile(request.RemoteFile);

                // Create a FTP request to upload file
                var localFileInf = new FileInfo(request.LocalFile);
                var ftpRequest   = (FtpWebRequest)WebRequest.Create(request.RemoteFile);
                ftpRequest.Credentials   = new NetworkCredential(_userId, _password);
                ftpRequest.Method        = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.UseBinary     = true;
                ftpRequest.UsePassive    = _usePassive;
                ftpRequest.ContentLength = localFileInf.Length;

                // Open ftp and local streams
                using (var localFileStream = localFileInf.OpenRead())
                    using (var ftpRequestStream = ftpRequest.GetRequestStream())
                    {
                        // Write Content from the file stream to the FTP Upload Stream
                        const int bufferLength           = 2048;
                        var       buffer                 = new byte[bufferLength];
                        var       localFileContentLength = localFileStream.Read(buffer, 0, bufferLength);
                        while (localFileContentLength != 0)
                        {
                            ftpRequestStream.Write(buffer, 0, localFileContentLength);
                            localFileContentLength = localFileStream.Read(buffer, 0, bufferLength);
                        }
                    }
            }
            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);
            }
        }
コード例 #7
0
		/// <summary>
		/// Upload one file from local to remote.
		/// </summary>
		/// <param name="request"></param>
		public void Upload(FileTransferRequest request)
		{
			try
			{
				CreateRemoteDirectoryForFile(request.RemoteFile);

				// Create a FTP request to upload file
				var localFileInf = new FileInfo(request.LocalFile);
				var ftpRequest = (FtpWebRequest)WebRequest.Create(request.RemoteFile);
				ftpRequest.Credentials = new NetworkCredential(_userId, _password);
				ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
				ftpRequest.UseBinary = true;
				ftpRequest.UsePassive = _usePassive;
				ftpRequest.ContentLength = localFileInf.Length;

				// Open ftp and local streams
				using (var localFileStream = localFileInf.OpenRead())
				using (var ftpRequestStream = ftpRequest.GetRequestStream())
				{
					// Write Content from the file stream to the FTP Upload Stream
					const int bufferLength = 2048;
					var buffer = new byte[bufferLength];
					var localFileContentLength = localFileStream.Read(buffer, 0, bufferLength);
					while (localFileContentLength != 0)
					{
						ftpRequestStream.Write(buffer, 0, localFileContentLength);
						localFileContentLength = localFileStream.Read(buffer, 0, bufferLength);
					}
				}
			}
			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);
			}
		}
コード例 #8
0
        /// <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);
            }
        }
コード例 #9
0
ファイル: FtpFileTransfer.cs プロジェクト: nhannd/Xian
		/// <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);
			}
		}
コード例 #10
0
		/// <summary>
		/// Download one file from remote to local
		/// </summary>
		/// <param name="request"></param>
		public void Download(FileTransferRequest request)
		{
			try
			{
				// Create a FTP request to download file
				var ftpRequest = (FtpWebRequest) WebRequest.Create(request.RemoteFile);
				ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
				ftpRequest.UseBinary = true;
				ftpRequest.UsePassive = _usePassive;
				ftpRequest.Credentials = new NetworkCredential(_userId, _password);

				// Create download directory if not already exist
				var downloadDirectory = Path.GetDirectoryName(request.LocalFile);
				if (!Directory.Exists(downloadDirectory))
					Directory.CreateDirectory(downloadDirectory);

				// Open ftp and local streams

				using (var ftpResponse = (FtpWebResponse) ftpRequest.GetResponse())
				using (var ftpResponseStream = ftpResponse.GetResponseStream())
				using (var localFileStream = new FileStream(request.LocalFile, FileMode.Create))
				{
					// Write Content from the FTP download stream to local file stream
					const int bufferSize = 2048;
					var buffer = new byte[bufferSize];
					var readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
					while (readCount > 0)
					{
						localFileStream.Write(buffer, 0, readCount);
						readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
					}
				}
			}
			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);
			}
		}