Displaying file upload progress in ProgressBar using SSH.NET
I want to show the progress of the download process on my ProgressBar
. I tried to do something like this code to download , but I failed. Here is an example of 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
source to share
As you did it correctly, similar to the code for displaying the progress of a file upload , you must provide a callback to the argument downloadCallback
for . SftpClient.DownloadFile
public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)
Also you are loading the background thread correctly. Alternatively, you can use asynchronous loading ( ). SftpClient.BeginDownloadFile
What is wrong and needs to be changed:
- You have to open / create a local file to write (
FileMode.Create
). - You should get the size of the remote file, not the local one (not existing yet). Use .
SftpClient.GetAttributes
An example of 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.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; });
}
For upload see
Displaying file upload progress in ProgressBar using SSH.NET
source to share