Пример #1
0
        // up- or downloads next file from file list
        private void TransferNextFile()
        {
            string fileName    = fileEnumerator.Current.Name;
            string destination = fileEnumerator.Current.Destination;

            // check if progress should be displayed
            if (showProgress)
            {
                lastTransferred = 0;
                SetCurrentFile(fileName);
                SetCurrentProgress(0);
            }

            try
            {
                string dir = "";

                if (destination.IndexOf('/') > 0)
                {
                    dir = destination.Substring(0, destination.LastIndexOf('/'));
                }

                // check if file should be up- or downloaded
                if (download)
                {
                    // create directory if doesn't exist
                    if (!Directory.Exists(localPath + dir))
                    {
                        Directory.CreateDirectory(localPath + dir);
                    }

                    // download file
                    Client.DownloadFileAsync(new Uri(remotePath + fileName), localPath + destination);
                }
                else
                {
                    // check if directory was created before
                    if (!directories.Contains(remotePath + dir))
                    {
                        // check if directory exists
                        if (!DirectoryExists(remotePath + dir))
                        {
                            CreateDirectory(dir, remotePath);
                        }
                        directories.Add(remotePath + dir);
                    }

                    // upload file
                    Client.UploadFileAsync(new Uri(remotePath + destination), localPath + fileName);
                }
            }
            catch (Exception e)
            {
                ErrorLog.Add(this, e.Message);
                CleanUp();
                callback.Invoke(FileTransferResult.Failure);
            }
        }
Пример #2
0
 // sends an request to the remote server (async)
 private void SendRequestAsync(string name, Uri remotePath, FileTransferCallback callback, DelegateRequest caller)
 {
     try
     {
         caller.BeginInvoke(name, remotePath,
                            delegate(IAsyncResult iar)
         {
             var c = (DelegateRequest)iar.AsyncState;
             c.EndInvoke(iar);
             callback.Invoke(FileTransferResult.Success);
         }, caller);
     }
     catch (Exception e)
     {
         ErrorLog.Add(this, e.Message);
         callback.Invoke(FileTransferResult.Failure);
     }
 }
Пример #3
0
        // up- or downloads files (async)
        private bool TransferFilesAsync(List <TransferFile> files, bool showProgress,
                                        Uri remotePath, string localPath, FileTransferCallback callback, bool download)
        {
            // set busy flag
            lock (this)
            {
                if (busy)
                {
                    return(false);
                }

                if (files.Count == 0)
                {
                    callback.Invoke(FileTransferResult.Success);
                    return(true);
                }

                busy = true;
            }

            totalTransferred = 0;
            totalSize        = 0;

            // calculate total size
            foreach (TransferFile file in files)
            {
                totalSize += file.Size;
            }

            this.files        = files;
            this.remotePath   = remotePath;
            this.localPath    = localPath;
            this.showProgress = showProgress;
            this.callback     = callback;
            this.download     = download;

            // select next file
            fileEnumerator = files.GetEnumerator();
            fileEnumerator.MoveNext();

            // check if progress should be displayed
            if (showProgress)
            {
                EnableProgress();
                buffer.Clear();
                startTime = DateTime.Now;
                progressTimer.Start();
            }

            // up- or download next file
            TransferNextFile();
            return(true);
        }