2017-06-08 148 views
2

我想在我的ProgressBar上显示下载过程的进度。我试图做一些像this code for upload这样的事情,但我失败了。这里是我的失败尝试的例子用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; 
     }); 
} 

预先感谢您

回答

4

正如你正确地做,为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"; 

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

enter image description here


对于上传见:
Displaying progress of file upload in a ProgressBar with SSH.NET

+0

'SftpClient.GetAttributes'这正是我所期待的。非常感谢 – Yox