使用SSH.NET在ProgressBar中显示文件下载的进度 [英] Displaying progress of file download in a ProgressBar with SSH.NET

查看:159
本文介绍了使用SSH.NET在ProgressBar中显示文件下载的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ProgressBar 上显示下载过程的进度。我试图做类似






有关上传的信息,请参见:

使用SSH.NET在ProgressBar中显示文件上传的进度


I want to show the progress of a downloading process on my ProgressBar. I tried to do somethings like this code for upload, but I failed. Here is an example of my failed attempts

private void button5_Click(object sender, EventArgs e)
{
    Task.Run(() => Download());
}

private void Download()
{
    try
    {
        int Port = (int)numericUpDown1.Value;
        string Host = comboBox1.Text;
        string Username = textBox3.Text;
        string Password = textBox4.Text;
        string SourcePath = textBox5.Text;
        string RemotePath = textBox6.Text;
        string FileName = textBox7.Text;

        using (var file = File.OpenWrite(SourcePath + FileName))
        using (var Stream = new FileStream(SourcePath + FileName, FileMode.Open))
        using (var Client = new SftpClient(Host, Port, Username, Password))
        {
            Client.Connect();
            progressBar1.Invoke((MethodInvoker)
            delegate
            {
                progressBar1.Maximum = (int)Stream.Length;
            });
            Client.DownloadFile(RemotePath + FileName, /*file*/ Stream, DownloadProgresBar);
            Client.Disconnect();
        }
    }
    catch (Exception Ex)
    {
        System.Windows.Forms.MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
private void DownloadProgresBar(ulong Downloaded)
{
    progressBar1.Invoke((MethodInvoker)
        delegate
        {
            progressBar1.Value = (int)Downloaded;
        });
}

Thank you in advance

解决方案

As you correctly did, similarly to the code for displaying progress of file upload, you have to provide a callback to the downloadCallback argument of SftpClient.DownloadFile.

public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)

Also you correctly download on a background thread. Alternatively, you could use an asynchronous upload (SftpClient.BeginDownloadFile).

What is wrong and needs change:

  • You have to open/create the local file for writing (FileMode.Create).
  • You have to retrieve a size of the remote file, not the local one (not existing yet). Use SftpClient.GetAttributes.

Example using a background thread (task):

private void button1_Click(object sender, EventArgs e)
{
    // Run Download on background thread
    Task.Run(() => Download());
}

private void Download()
{
    try
    {
        int Port = 22;
        string Host = "example.com";
        string Username = "username";
        string Password = "password";
        string RemotePath = "/remote/path/";
        string SourcePath = @"C:\local\path\";
        string FileName = "download.txt";

        using (var stream = new FileStream(SourcePath + FileName, FileMode.Create))
        using (var client = new SftpClient(Host, Port, Username, Password))
        {
            client.Connect();
            SftpFileAttributes attributes = client.GetAttributes(RemotePath + FileName);
            // Set progress bar maximum on foreground thread
            progressBar1.Invoke(
                (MethodInvoker)delegate { progressBar1.Maximum = (int)attributes.Size; });
            // Download with progress callback
            client.DownloadFile(RemotePath + FileName, stream, DownloadProgresBar);
            MessageBox.Show("Download complete");
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

private void DownloadProgresBar(ulong uploaded)
{
    // Update progress bar on foreground thread
    progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = (int)uploaded; });
}


For upload see:
Displaying progress of file upload in a ProgressBar with SSH.NET

这篇关于使用SSH.NET在ProgressBar中显示文件下载的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆