/// <summary> /// 如果是一个错误将记录ErrorOccurred事件。 /// </summary> void client_ErrorOccurred(object sender, ErrorEventArgs e) { this.lstLog.Items.Add( string.Format("{0} {1} ", DateTime.Now, e.ErrorException.Message)); var innerException = e.ErrorException.InnerException; // 记录所有的innerException. while (innerException != null) { this.lstLog.Items.Add( string.Format("\t\t\t {0} ", innerException.Message)); innerException = innerException.InnerException; } this.lstLog.SelectedIndex = this.lstLog.Items.Count - 1; }
/// <summary> /// Download a single file directly. /// </summary> void DownloadFile(FTPFileSystem file, string localPath) { if (file.IsDirectory) { throw new ArgumentException( "The FTPFileSystem to download is a directory in fact"); } string destPath = localPath + "\\" + file.Name; // Create a request to the file to be downloaded. FtpWebRequest request = WebRequest.Create(file.Url) as FtpWebRequest; request.Credentials = this.manager.Credentials; // Download file. request.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse response = null; Stream responseStream = null; MemoryStream downloadCache = null; try { // Retrieve the response from the server and get the response stream. response = request.GetResponse() as FtpWebResponse; this.manager.OnNewMessageArrived(new NewMessageEventArg { NewMessage = response.StatusDescription }); responseStream = response.GetResponseStream(); // Cache data in memory. downloadCache = new MemoryStream(FTPDownloadClient.MaxCacheSize); byte[] downloadBuffer = new byte[FTPDownloadClient.BufferSize]; int bytesSize = 0; int cachedSize = 0; // Download the file until the download is completed. while (true) { // Read a buffer of data from the stream. bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length); // If the cache is full, or the download is completed, write // the data in cache to local file. if (bytesSize == 0 || MaxCacheSize < cachedSize + bytesSize) { try { // Write the data in cache to local file. WriteCacheToFile(downloadCache, destPath, cachedSize); // Stop downloading the file if the download is paused, // canceled or completed. if (bytesSize == 0) { break; } // Reset cache. downloadCache.Seek(0, SeekOrigin.Begin); cachedSize = 0; } catch (Exception ex) { string msg = string.Format( "There is an error while downloading {0}. " + " See InnerException for detailed error. ", file.Url); ApplicationException errorException = new ApplicationException(msg, ex); // Fire the DownloadCompleted event with the error. ErrorEventArgs e = new ErrorEventArgs { ErrorException = errorException }; this.manager.OnErrorOccurred(e); return; } } // Write the data from the buffer to the cache in memory. downloadCache.Write(downloadBuffer, 0, bytesSize); cachedSize += bytesSize; } var fileDownloadCompletedEventArgs = new FileDownloadCompletedEventArgs { LocalFile = new FileInfo(destPath), ServerPath = file.Url }; this.OnFileDownloadCompleted(fileDownloadCompletedEventArgs); } finally { if (response != null) { response.Close(); } if (responseStream != null) { responseStream.Close(); } if (downloadCache != null) { downloadCache.Close(); } } }
/// <summary> /// Log the ErrorOccurred event if there was an error. /// </summary> void client_ErrorOccurred(object sender, ErrorEventArgs e) { this.Invoke(new EventHandler <ErrorEventArgs>( client_ErrorOccurredHandler), sender, e); }
/// <summary> /// 下载一个单一的文件目录. /// </summary> void DownloadFile(FTPFileSystem file, string localPath) { if (file.IsDirectory) { throw new ArgumentException( "The FTPFileSystem to download is a directory in fact"); } string destPath = localPath + "\\" + file.Name; //创建一个要下载的文件的请求. FtpWebRequest request = WebRequest.Create(file.Url) as FtpWebRequest; request.Credentials = this.manager.Credentials; // 下载文件. request.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse response = null; Stream responseStream = null; MemoryStream downloadCache = null; try { //检索来自服务器的响应,并得到响应流. response = request.GetResponse() as FtpWebResponse; this.manager.OnNewMessageArrived(new NewMessageEventArg { NewMessage = response.StatusDescription }); responseStream = response.GetResponseStream(); //内存中的缓存数据. downloadCache = new MemoryStream(FTPDownloadClient.MaxCacheSize); byte[] downloadBuffer = new byte[FTPDownloadClient.BufferSize]; int bytesSize = 0; int cachedSize = 0; // 下载这个文件直到下载完成. while (true) { //从流中读取数据的缓冲区。 bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length); //如果这个缓存区是空的,或者下载完成,把在缓存中的数据写到本地文件中。 if (bytesSize == 0 || MaxCacheSize < cachedSize + bytesSize) { try { // 把在缓存中的数据写到本地文件中. WriteCacheToFile(downloadCache, destPath, cachedSize); //如果被暂停下载将停止正在下载的文件,取消或者完成. if (bytesSize == 0) { break; } // 重置缓存. downloadCache.Seek(0, SeekOrigin.Begin); cachedSize = 0; } catch (Exception ex) { string msg = string.Format( "There is an error while downloading {0}. " + " See InnerException for detailed error. ", file.Url); ApplicationException errorException = new ApplicationException(msg, ex); //这个错误给了DownloadCompleted事件 ErrorEventArgs e = new ErrorEventArgs { ErrorException = errorException }; this.manager.OnErrorOccurred(e); return; } } //把缓冲区的数据写到内存中的缓存。 downloadCache.Write(downloadBuffer, 0, bytesSize); cachedSize += bytesSize; } var fileDownloadCompletedEventArgs = new FileDownloadCompletedEventArgs { LocalFile = new FileInfo(destPath), ServerPath = file.Url }; this.OnFileDownloadCompleted(fileDownloadCompletedEventArgs); } finally { if (response != null) { response.Close(); } if (responseStream != null) { responseStream.Close(); } if (downloadCache != null) { downloadCache.Close(); } } }