2017-06-08 2 views
2

Je veux montrer la progression d'un processus de téléchargement sur mon ProgressBar. J'ai essayé de faire quelque chose comme this code for upload, mais j'ai échoué. Voici un exemple de mes tentatives infructueusesAffichage de la progression du téléchargement de fichier dans un ProgressBar avec SSH.NET

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

Nous vous remercions à l'avance

Répondre

4

Comme vous l'avez fait correctement, de façon similaire au code displaying progress of file upload, vous devez fournir un rappel à l'argument downloadCallback de SftpClient.DownloadFile.

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

Vous avez également téléchargé correctement sur un arrière-plan. Vous pouvez également utiliser un téléchargement asynchrone (SftpClient.BeginDownloadFile).

Quel est le problème et a besoin de vous changer:

  • Vous devez ouvrir/créer le fichier local pour écrire (FileMode.Create).
  • Vous devez récupérer une taille du fichier distant, pas la taille locale (pas encore disponible). Utilisez SftpClient.GetAttributes.

Exemple avec un fil d'arrière-plan (tâche):

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


Pour téléchargement voir:
Displaying progress of file upload in a ProgressBar with SSH.NET

+0

'SftpClient.GetAttributes' C'est exactement ce que je cherchais. Merci beaucoup – Yox