private void DownloadAsync()
        {
            var ftpClient = this.GetFtpClient();
            Task.Factory.StartNew(() =>
            {
                using (ftpClient)
                {
                    var foo = ftpClient.GetCurrentFtpDirectory();
                    long size;
                    using (var memoryStream = new MemoryStream())
                    {
                        {
                            var ftpFile = new FtpFile("/pub/mozilla/nightly/README");
                            var success = ftpClient.Download(ftpFile,
                                                             memoryStream);
                            if (!success)
                            {
                                size = -1;
                            }
                            else
                            {
                                size = memoryStream.Length;
                            }
                        }
                        {
                            var ftpFile = new FtpFile("/pub/README");
                            var success = ftpClient.Download(ftpFile,
                                                             memoryStream);
                        }
                    }

                    this.Dispatcher.BeginInvoke(() =>
                    {
                        var messageBoxText = string.Format("size: {0}",
                                                           size);
                        MessageBox.Show(messageBoxText);
                    });
                }
            });
        }
        public async Task<bool> DeleteAsync(FtpFile ftpFile,
                                            CancellationToken cancellationToken)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);
                if (controlComplexSocket == null)
                {
                    return false;
                }

                var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                  ftpFile,
                                                                  cancellationToken);
                if (!success)
                {
                    return false;
                }

                success = await this.SendAndLogAsync(controlComplexSocket,
                                                     cancellationToken,
                                                     "DELE {0}",
                                                     ftpFile.FileName);
                if (!success)
                {
                    return false;
                }

                var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);
                if (!ftpReply.Success)
                {
                    return false;
                }
            }

            return true;
        }
        private void UploadAsync()
        {
            var ftpClient = this.GetFtpClient();
            ThreadPool.QueueUserWorkItem(callBack =>
            {
                var value = "hallo ich bin's ... wer bist'n du??";
                var bytes = Encoding.UTF8.GetBytes(value);
                var ftpFile = new FtpFile("/test12/hello.txt");

                using (ftpClient)
                {
                    bool success;
                    var memoryStream = new MemoryStream(bytes);
                    using (memoryStream)
                    {
                        success = ftpClient.Upload(memoryStream,
                                                   ftpFile);
                    }
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        var messageBoxText = string.Format("success: {0}",
                                                           success);
                        MessageBox.Show(messageBoxText);
                    });
                }
            });
        }
 public bool Delete(FtpFile ftpFile,
                    CancellationToken cancellationToken)
 {
     return this.DeleteAsync(ftpFile,
                             cancellationToken)
                .Result;
 }
 public async Task<bool> DeleteAsync(FtpFile ftpFile)
 {
     return await this.DeleteAsync(ftpFile,
                                   CancellationToken.None);
 }
        public async Task<bool> DownloadAsync(FtpFile ftpFile,
                                              Stream stream,
                                              CancellationToken cancellationToken)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);
                if (controlComplexSocket == null)
                {
                    return false;
                }

                {
                    var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                      ftpFile,
                                                                      cancellationToken);
                    if (!success)
                    {
                        return false;
                    }
                }

                long bytesTotal;

                // sending SIZE
                // reading SIZE
                {
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "SIZE {0}",
                                                             ftpFile.FileName);
                    if (!success)
                    {
                        return false;
                    }

                    var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);
                    if (!ftpReply.Success)
                    {
                        return false;
                    }

                    if (!long.TryParse(ftpReply.ResponseMessage,
                                       out bytesTotal))
                    {
                        return false;
                    }
                }

                // sending PASV
                // reading PASV
                var transferComplexSocket = await this.GetPassiveComplexSocketAsync(controlComplexSocket,
                                                                                    cancellationToken);
                if (transferComplexSocket == null)
                {
                    return false;
                }

                {
                    FtpReply ftpReply;

                    using (transferComplexSocket)
                    {
                        // sending RETR
                        // open transfer socket
                        // reading RETR (150 Opening BINARY mode data connection...)
                        var success = await this.SendAndLogAsync(controlComplexSocket,
                                                                 cancellationToken,
                                                                 "RETR {0}",
                                                                 ftpFile.FileName);
                        if (!success)
                        {
                            return false;
                        }

                        success = await transferComplexSocket.ConnectAsync(cancellationToken);
                        if (!success)
                        {
                            return false;
                        }

                        ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);
                        if (!ftpReply.Success)
                        {
                            return false;
                        }

                        {
                            this.WaitBeforeReceive();

                            // reading transfer socket
                            var rawFtpResponse = await transferComplexSocket.Socket.ReceiveAsync(this.ChunkReceiveBufferSize,
                                                                                                 transferComplexSocket.GetSocketAsyncEventArgs,
                                                                                                 cancellationToken,
                                                                                                 bytesTotal,
                                                                                                 bytesReceived =>
                                                                                                 {
                                                                                                     var downloadProgressEventArgs = new DownloadProgressEventArgs(bytesReceived,
                                                                                                                                                                   bytesTotal);
                                                                                                     this.OnDownloadProgressAsync(downloadProgressEventArgs);
                                                                                                 });
                            if (!rawFtpResponse.Success)
                            {
                                return false;
                            }

                            stream.Write(rawFtpResponse.Buffer,
                                         0,
                                         rawFtpResponse.Buffer.Length);
                        }
                    }

                    if (!ftpReply.Completed)
                    {
                        // reading RETR (226 Transfer complete)
                        ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);
                        if (!ftpReply.Success)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
 public bool Delete(FtpFile ftpFile)
 {
     return this.Delete(ftpFile,
                        CancellationToken.None);
 }
 public bool Download(FtpFile ftpFile,
                      Stream stream,
                      CancellationToken cancellationToken)
 {
     return this.DownloadAsync(ftpFile,
                               stream,
                               cancellationToken)
                .Result;
 }
 public async Task<bool> DownloadAsync(FtpFile ftpFile,
                                       Stream stream)
 {
     return await this.DownloadAsync(ftpFile,
                                     stream,
                                     CancellationToken.None);
 }
 public bool Download(FtpFile ftpFile,
                      Stream stream)
 {
     return this.Download(ftpFile,
                          stream,
                          CancellationToken.None);
 }
        public async Task<bool> UploadAsync(Stream stream,
                                            FtpFile ftpFile,
                                            CancellationToken cancellationToken,
                                            bool createDirectoryIfNotExists = true)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);
                if (controlComplexSocket == null)
                {
                    return false;
                }

                {
                    var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                      ftpFile,
                                                                      cancellationToken,
                                                                      createDirectoryIfNotExists);
                    if (!success)
                    {
                        return false;
                    }
                }

                // sending PASV
                // reading PASV
                var transferComplexSocket = await this.GetPassiveComplexSocketAsync(controlComplexSocket,
                                                                                    cancellationToken);
                if (transferComplexSocket == null)
                {
                    return false;
                }

                FtpReply ftpReply;
                using (transferComplexSocket)
                {
                    // sending STOR
                    // open transfer socket
                    // reading STOR (150 ...)
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "STOR {0}",
                                                             ftpFile.FileName);
                    if (!success)
                    {
                        return false;
                    }

                    success = await transferComplexSocket.ConnectAsync(cancellationToken);
                    if (!success)
                    {
                        return false;
                    }

                    ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);
                    if (!ftpReply.Success)
                    {
                        return false;
                    }

                    {
                        // sending transfer socket
                        success = await transferComplexSocket.Socket.SendAsync(this.ChunkSendBufferSize,
                                                                               controlComplexSocket.GetSocketAsyncEventArgs,
                                                                               stream,
                                                                               cancellationToken,
                                                                               (bytesSent,
                                                                                bytesTotal) =>
                                                                               {
                                                                                   var uploadProgressEventArgs = new UploadProgressEventArgs(bytesSent,
                                                                                                                                             bytesTotal);
                                                                                   this.OnUploadProgressAsync(uploadProgressEventArgs);
                                                                               });
                        if (!success)
                        {
                            return false;
                        }
                    }
                }

                if (!ftpReply.Completed)
                {
                    // reading STOR (226 ...)
                    ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);
                    if (!ftpReply.Success)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
 public async Task<bool> UploadAsync(Stream stream,
                                     FtpFile ftpFile,
                                     bool createDirectoryIfNotExists = true)
 {
     return await this.UploadAsync(stream,
                                   ftpFile,
                                   CancellationToken.None,
                                   createDirectoryIfNotExists);
 }
 public bool Upload(Stream stream,
                    FtpFile ftpFile,
                    CancellationToken cancellationToken,
                    bool createDirectoryIfNotExists = true)
 {
     return this.UploadAsync(stream,
                             ftpFile,
                             cancellationToken,
                             createDirectoryIfNotExists)
                .Result;
 }
 public bool Upload(Stream stream,
                    FtpFile ftpFile,
                    bool createDirectoryIfNotExists = true)
 {
     return this.Upload(stream,
                        ftpFile,
                        CancellationToken.None,
                        createDirectoryIfNotExists);
 }