private void StartFtpUploadInThread() { try { Thread.Sleep(50); String host = ftpDestNode.Site.host; String username = ftpDestNode.Site.username; String pwd = ftpDestNode.Site.pwd; String targetfolder = ftpDestNode.Path; if (sourceFiles != null) { int count = sourceFiles.Count(); int iFileCount = 0; foreach (var file in sourceFiles) { string basicProgress = (++iFileCount).ToString() + " of " + count.ToString() + " : " + file.Name; progress.SetProgress(basicProgress); if (this.cancelled) { break; } FtpWebResponse response = null; FileStream inputStream = null; Stream ftpStream = null; while (!this.cancelled) { bool success = false; Exception lastException = null; for (int i = 0; i < 4 && !this.cancelled; i++) { try { progress.SetProgress(basicProgress + " - Connecting"); lock (FtpSite.LockInstance()) { string ftpfullpath = "ftp://" + host + targetfolder + "/" + file.Name; FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); ftp.UseBinary = true; ftp.KeepAlive = true; ftp.Method = WebRequestMethods.Ftp.UploadFile; ftp.Timeout = 2000; ftp.Credentials = new NetworkCredential(username, pwd); response = (FtpWebResponse)ftp.GetResponse(); inputStream = File.OpenRead(file.FullName); // ftpStream = response.GetResponseStream(); ftpStream = ftp.GetRequestStream(); progress.SetProgress(basicProgress + " - Reponse Recieved"); int bufferSize = 2048 * 2 * 2 * 2 * 2 * 2 * 2; int readCount; int totalCount = 0; byte[] buffer = new byte[bufferSize]; readCount = inputStream.Read(buffer, 0, bufferSize); while (readCount > 0 & !this.cancelled) { totalCount += readCount; progress.SetProgress(basicProgress + " - Uploading: " + totalCount.ToString() ); ftpStream.Write(buffer, 0, readCount); readCount = inputStream.Read(buffer, 0, bufferSize); } success = true; progress.SetProgress(basicProgress + " - Completed"); } break; } catch (Exception e) { lastException = e; } finally { try { if (ftpStream != null) { ftpStream.Close(); } } catch (Exception) { } try { if (inputStream != null) { inputStream.Close(); } } catch (Exception) { } try { if (response != null) { response.Close(); } } catch (Exception) { } } } if (this.cancelled) { return; } if (!success) { DialogResult keepGoing = MessageBox.Show("Keep Trying? Failed after 4 attempts to download: " + file + " Exception: " + lastException.Message , "FTP Failure" , MessageBoxButtons.OKCancel , MessageBoxIcon.Error , MessageBoxDefaultButton.Button1); if (keepGoing != DialogResult.OK) { return; // Don't true any more. } } if (success) { break; // Goto next file only on success. } } } } } finally { progress.DoClose(); } }
private void StartFtpDeleteInThread() { try { Thread.Sleep(50); String host = folder.Site.host; String username = folder.Site.username; String pwd = folder.Site.pwd; String targetfolder = folder.Path; if (files != null) { int count = files.Count(); int iFileCount = 0; foreach (var file in files) { string basicProgress = (++iFileCount).ToString() + " of " + count.ToString() + " : " + file; progress.SetProgress(basicProgress); if (this.cancelled) { break; } FtpWebResponse response = null; while (!this.cancelled) { bool success = false; Exception lastException = null; for (int i = 0; i < 4 && !this.cancelled; i++) { try { progress.SetProgress(basicProgress + " - Connecting"); lock (FtpSite.LockInstance()) { string ftpfullpath = "ftp://" + host + targetfolder + "/" + file; FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); ftp.UseBinary = true; ftp.KeepAlive = true; ftp.Method = WebRequestMethods.Ftp.DeleteFile; ftp.Timeout = 2000; ftp.Credentials = new NetworkCredential(username, pwd); response = (FtpWebResponse)ftp.GetResponse(); progress.SetProgress(basicProgress + response.StatusDescription); success = true; } break; } catch (Exception e) { lastException = e; } finally { try { if (response != null) { response.Close(); } } catch (Exception) { } } } if (this.cancelled) { return; } if (!success) { DialogResult keepGoing = MessageBox.Show("Keep Trying? Failed after 4 attempts to delete: " + file + " Exception: " + lastException.Message , "FTP Failure" , MessageBoxButtons.OKCancel , MessageBoxIcon.Error , MessageBoxDefaultButton.Button1); if (keepGoing != DialogResult.OK) { return; // Don't true any more. } } if (success) { break; // Goto next file only on success. } } } } } finally { progress.DoClose(); } }