Exemplo n.º 1
0
        private void StartFtpDownLoadInThread()
        {
            try
            {
                Thread.Sleep(50);

                String host     = sourceNode.Site.host;
                String username = sourceNode.Site.username;
                String pwd      = sourceNode.Site.pwd;

                String targetfolder = sourceNode.Path;

                int count      = files.Count();
                int iFileCount = 0;

                foreach (string file in files)
                {
                    string basicProgress = (++iFileCount).ToString() + " of " + count.ToString() + " : " + file;
                    progress.SetProgress(basicProgress);
                    if (this.cancelled)
                    {
                        break;
                    }


                    FtpWebResponse response     = null;
                    FileStream     outputStream = 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;
                                    FtpWebRequest ftp         = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);

                                    ftp.UseBinary = true;
                                    ftp.KeepAlive = true;
                                    ftp.Method    = WebRequestMethods.Ftp.DownloadFile;
                                    ftp.Timeout   = 2000;

                                    ftp.Credentials = new NetworkCredential(username, pwd);

                                    response = (FtpWebResponse)ftp.GetResponse();

                                    outputStream = new FileStream(Path.Combine(destNode.FullPath, file), FileMode.Create);

                                    ftpStream = response.GetResponseStream();

                                    progress.SetProgress(basicProgress + " - Reponse Recieved");

                                    long   cl         = response.ContentLength;
                                    int    bufferSize = 2048 * 2 * 2 * 2 * 2 * 2 * 2;
                                    int    readCount;
                                    int    totalCount = 0;
                                    byte[] buffer     = new byte[bufferSize];

                                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                                    while (readCount > 0 & !this.cancelled)
                                    {
                                        totalCount += readCount;
                                        progress.SetProgress(basicProgress + " - Downloading: " + totalCount.ToString()
                                                             );

                                        outputStream.Write(buffer, 0, readCount);
                                        readCount = ftpStream.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 (outputStream != null)
                                      {
                                          outputStream.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();
            }
        }
Exemplo n.º 2
0
        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();
            }
        }