Renci.SshNet.SftpClient.BeginDownloadFile C# (CSharp) Method

BeginDownloadFile() public method

Begins an asynchronous file downloading into the stream.
Method calls made by this method to output, may under certain conditions result in exceptions thrown by the stream.
is null. is null or contains only whitespace characters. The method was called after the client was disposed.
public BeginDownloadFile ( string path, Stream output, AsyncCallback asyncCallback, object state, Action downloadCallback = null ) : IAsyncResult
path string The path.
output Stream The output.
asyncCallback AsyncCallback The method to be called when the asynchronous write operation is completed.
state object A user-provided object that distinguishes this particular asynchronous write request from other requests.
downloadCallback Action The download callback.
return IAsyncResult
        public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null)
        {
            CheckDisposed();

            if (path.IsNullOrWhiteSpace())
                throw new ArgumentException("path");

            if (output == null)
                throw new ArgumentNullException("output");

            var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);

            ThreadAbstraction.ExecuteThread(() =>
            {
                try
                {
                    InternalDownloadFile(path, output, asyncResult, offset =>
                    {
                        asyncResult.Update(offset);

                        if (downloadCallback != null)
                        {
                            downloadCallback(offset);
                        }
                    });

                    asyncResult.SetAsCompleted(null, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return asyncResult;
        }

Same methods

SftpClient::BeginDownloadFile ( string path, Stream output ) : IAsyncResult
SftpClient::BeginDownloadFile ( string path, Stream output, AsyncCallback asyncCallback ) : IAsyncResult

Usage Example

Ejemplo n.º 1
1
                private void DownloadFilesFromServer()
                {
                    try
                    {
                        var filelist = new Dictionary<SftpFile, string>();
                        var remfold = txtRemoteFolderPath.Text.EndsWith("/") ? txtRemoteFolderPath.Text : txtRemoteFolderPath.Text += "/";
                        var ssh = new SftpClient(txtHost.Text, int.Parse(txtPort.Text), txtUser.Text, txtPassword.Text);
                        ssh.Connect();
                        foreach (var i in lvSSHFileBrowser.SelectedItems.Cast<EXImageListViewItem>().Select(item => item.Tag as SftpFile))
                        {
                            if (i.IsRegularFile)
                            {
                                filelist.Add(i, Path.Combine(txtLocalBrowserPath.Text, i.Name));
                            }
                            if (i.IsDirectory)
                            {
                                foreach (var file in GetFilesRecur(ssh, i))
                                {
                                    var i1 = file.FullName.Replace(remfold, "");
                                    var i2 = i1.Replace('/', '\\');
                                    var i3 = Path.Combine(txtLocalBrowserPath.Text, i2);
                                    filelist.Add(file, i3);
                                }
                            }
                        }
                        long totalsize = filelist.Sum(pair => pair.Key.Length);
                        var result = MessageBox.Show(string.Format(Language.SSHTransfer_DownloadFilesFromServer_Download__0__in__1__files_ + "\r\n" + Language.SSHTransfer_DownloadFilesFromServer_Destination_directory__2_, Tools.Misc.LengthToHumanReadable(totalsize), filelist.Count, txtLocalBrowserPath.Text), "Downloading", MessageBoxButtons.YesNoCancel);
                        if (result!=DialogResult.Yes)
                        {
                            EnableButtons();
                            return;
                        }
                        long totaluploaded = 0;
                        ThreadPool.QueueUserWorkItem(state =>
                            {
                                foreach (var file in filelist)
                                {
                                    if (!Directory.Exists(Path.GetDirectoryName(file.Value)))
                                    {
                                        Tools.Misc.ebfFolderCreate(Path.GetDirectoryName(file.Value));
                                    }
                                    var asynch = ssh.BeginDownloadFile(file.Key.FullName,
                                                                       new FileStream(file.Value, FileMode.Create));
                                    var sftpAsynch = asynch as SftpDownloadAsyncResult;

                                    while (!sftpAsynch.IsCompleted)
                                    {
                                        SetProgressStatus(totaluploaded + (long)sftpAsynch.DownloadedBytes, totalsize);
                                    }
                                    totaluploaded += file.Key.Length;
                                    ssh.EndDownloadFile(asynch);
                                }
                                EnableButtons();
                                ssh.Disconnect();
                                MessageBox.Show(Language.SSHTransfer_DownloadFilesFromServer_Download_finished);
                            });
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
All Usage Examples Of Renci.SshNet.SftpClient::BeginDownloadFile