c# - 使用 SSH.NET 在进度栏中显示文件下载进度

标签 c# .net winforms sftp ssh.net

我想在我的 ProgressBar 上显示下载过程的进度。我尝试做类似 this code for upload 的事情,但我失败了。这是我失败的尝试的示例

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

提前谢谢

最佳答案

正如您所做的那样,类似于 displaying progress of file upload 的代码,您必须为 SftpClient.DownloadFiledownloadCallback 参数提供回调。

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

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

哪里出了问题并且需要改变:

  • 您必须打开/创建本地文件以进行写入 (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; });
}

enter image description here

<小时/>

对于上传,请参阅:
Displaying progress of file upload in a ProgressBar with SSH.NET

关于c# - 使用 SSH.NET 在进度栏中显示文件下载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442714/

相关文章:

c# - 如何在 .NET 2.0 中获取 Environment.SpecialFolders.MyVideo 文件夹?

C# HttpWebRequest 验证/指定使用的密码

c# - 如何通过 Presenter 中的界面将我的 View 按钮数据绑定(bind)到模型

c# - 共享 Windows Phone 运行时组件

c# - 合并键值对列表

c# - .NET XML 反序列化

c# - 使用 IoC 将参数从 ViewModel 传递到另一个

C# 在winform中插入新项目后设置ListView项目被选中

c# - 我怎样才能得到我的应用程序的父进程的PID

c# - 从 OWIN Web API 访问 Azure Key Vault