Exemplo n.º 1
0
        private void ThreadForProcessQueue(object stateInfo)
        {
            try {
                while (_threadRunning && queue.Count > 0)
                {
                    object[] keys = new object[queue.Keys.Count];
                    queue.Keys.CopyTo(keys, 0);
                    fileinfo nextitem = (fileinfo)queue[keys[0]];
                    if (nextitem.mkdirFlag)
                    {
                        ftpObject.createRemoteDirectory(nextitem);
                    }
                    else
                    {
                        if (nextitem.direction == directionEnum.down)
                        {
                            ftpObject.download(nextitem);
                        }
                        else
                        {
                            ftpObject.upload(nextitem);
                        }
                    }

                    queue.Remove(nextitem.completeFileName);
                }
            }
            catch (WebException ex) {
                Console.WriteLine("Error!:" + ex.Data.ToString());
            }
            finally {
                _threadRunning = false;
                queue.Clear();
            }
        }
Exemplo n.º 2
0
        public void download(fileinfo file)
        {
            Console.WriteLine("...::: BEFORE DOWNLOAD :::...");
            System.IO.FileInfo info = new FileInfo(file.destination);
            if (info.Exists)
            {
                info.Delete();
            }
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(file.completeFileName);

            request.Method     = WebRequestMethods.Ftp.DownloadFile;
            request.UseBinary  = true;
            request.UsePassive = true;
            request.KeepAlive  = true;
            //request.Timeout = 600000;
            request.Credentials = new NetworkCredential(_username, _password);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream         rs       = response.GetResponseStream();
            //rs.ReadTimeout = 600000;
            //rs.WriteTimeout = 600000;
            FileStream fs       = info.OpenWrite();
            int        fileSize = Convert.ToInt32(response.ContentLength);

            try
            {
                long   length     = response.ContentLength;
                int    bufferSize = 4096;
                int    readCount;
                byte[] buffer = new byte[4096];

                readCount = rs.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    fs.Write(buffer, 0, readCount);
                    readCount = rs.Read(buffer, 0, bufferSize);
                    OnDownloadProgress(file.completeFileName, 66, fileSize);
                    if (_download)
                    {
                        break;
                    }
                }
                OnDownloadComplete(file.completeFileName);
            }
            catch (EndOfStreamException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                OnStatusChange("Error occured: " + ex.Data, 0, 0);
            }
            finally{
                Console.WriteLine("...::: CLOSE STREAMS :::...");
                fs.Close();
                rs.Close();
            }
        }
Exemplo n.º 3
0
        public void upload(fileinfo file)
        {
            System.IO.FileInfo info     = new FileInfo(file.completeFileName);
            string             filename = file.completeFileName.Substring(file.completeFileName.LastIndexOf(@"\") + 1);
            FtpWebRequest      request  = (FtpWebRequest)FtpWebRequest.Create(file.destination + "/" + filename);

            request.Credentials   = new NetworkCredential(_username, _password);
            request.Method        = WebRequestMethods.Ftp.UploadFile;
            request.UseBinary     = true;
            request.ContentLength = info.Length;

            int bufferSize = 4096;

            byte[] bytes    = new byte[bufferSize];
            int    read     = 0;
            long   totBytes = 0;
            Stream rs       = request.GetRequestStream();

            using (FileStream fs = info.OpenRead()) {
                try {
                    do
                    {
                        read = fs.Read(bytes, 0, bufferSize);
                        rs.Write(bytes, 0, read);
                        totBytes += read;
                        OnUploadProgress(file.completeFileName, totBytes);
                        Console.WriteLine("Загружено:" + totBytes);
                        OnUploadProgress(file.completeFileName, totBytes);
                        if (_download)
                        {
                            break;
                        }
                    } while (read == bufferSize);
                    OnUploadComplete(file.completeFileName);
                }
                catch (WebException ex) {
                    MessageBox.Show(ex.Message, "Ошибка при загрузке на сервер");
                }
                finally {
                    fs.Close();
                }
            }
            rs.Close();
            request = null;
            return;
        }
Exemplo n.º 4
0
 public string createRemoteDirectory(fileinfo file)
 {
     try
     {
         string        filename = file.completeFileName;
         FtpWebRequest request  = (FtpWebRequest)FtpWebRequest.Create(file.destination);
         request.Credentials = new NetworkCredential(_username, _password);
         request.Method      = WebRequestMethods.Ftp.MakeDirectory;
         request.UseBinary   = true;
         FtpWebResponse response = (FtpWebResponse)request.GetResponse();
         return(response.StatusDescription);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Ошибка создания папки." + ex.Message);
     }
     return("");
 }