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

编程入门 行业动态 更新时间:2024-10-26 22:30:21
本文介绍了使用SSH.NET在ProgressBar中显示文件下载的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在我的 ProgressBar 上显示下载过程的进度。我试图做类似

有关上传的信息,请参见: 使用SSH.NET在ProgressBar中显示文件上传的进度

I want to show the progress of a downloading process on my ProgressBar. I tried to do somethings like this code for upload, but I failed. Here is an example of my failed attempts

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

Thank you in advance

解决方案

As you correctly did, similarly to the code for displaying progress of file upload, you have to provide a callback to the downloadCallback argument of SftpClient.DownloadFile.

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

Also you correctly download on a background thread. Alternatively, you could use an asynchronous upload (SftpClient.BeginDownloadFile).

What is wrong and needs change:

  • You have to open/create the local file for writing (FileMode.Create).
  • You have to retrieve a size of the remote file, not the local one (not existing yet). Use SftpClient.GetAttributes.

Example using a background thread (task):

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

For upload see: Displaying progress of file upload in a ProgressBar with SSH.NET

更多推荐

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

本文发布于:2023-11-05 04:21:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   文件   SSH   NET   ProgressBar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!