2017-06-08 169 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