使用 SSH.NET 在 ProgressBar 中显示文件下载进度

Displaying progress of file download in a ProgressBar with SSH.NET

我想在 ProgressBar 上显示下载过程的进度。我试图做类似 的事情,但我失败了。这是我失败尝试的一个例子

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;
        });
}

提前致谢

正如您所做的那样,与 的代码类似,您必须提供对 SftpClient.DownloadFiledownloadCallback 参数的回调。

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

此外,您在后台线程上正确下载。或者,您可以使用异步上传 (SftpClient.BeginDownloadFile)。

有什么问题需要修改:

  • 您必须 open/create 写入本地文件 (FileMode.Create)。
  • 您必须检索远程文件的大小,而不是本地文件(尚不存在)。使用 SftpClient.GetAttributes.

使用后台线程(任务)的示例:

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";

        string SourceFilePath = SourcePath + FileName;
        using (var stream = new FileStream(SourceFilePath, FileMode.Create))
        using (var client = new SftpClient(Host, Port, Username, Password))
        {
            client.Connect();
            string RemoteFilePath = RemotePath + FileName;
            SftpFileAttributes attrs = client.GetAttributes(RemoteFilePath);
            // Set progress bar maximum on foreground thread
            int max = (int)attrs.Size;
            progressBar1.Invoke(
                (MethodInvoker)delegate { progressBar1.Maximum = max; });
            // Download with progress callback
            client.DownloadFile(RemoteFilePath, 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; });
}


对于上传见: