Unable to download files and show progress bar with SFTP?

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
I am creating a C# windows form application and I am trying to download files from a remote directory and store them into a local server. However, to make the application more interactive and user friendly, I am looking into adding a progress bar to show the start and finish of the download operation.

This what I am doing to download files from a folder to my device from a remote server. (all the information about host, username etc. are from a JSON file)

private void button6_Click_1(object sender, EventArgs e)
{
//timer1.Enabled = true;
Task.Run(() => Download());
filePath = @"D:\DataProfiler_Nautitech\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

//first data
string host = currentList[0].IPaddress;
string username = currentList[0].username;
string password = currentList[0].password;
string remoteDirectory = currentList[0].sourcefolder;
string localDirectory = currentList[0].destfolder;
string filextension = currentList[0].filextension;

using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
Console.WriteLine("Machine 1 - Connected");
var files = sftp.ListDirectory(remoteDirectory);

foreach (var file in files)
{
try
{
string remoteFileName = file.Name;

if ((file.Name.EndsWith(filextension)))
{
using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
{

string path = remoteDirectory + "/" + remoteFileName;
sftp.DownloadFile(path, file1);

}
}

}
catch (Exception er1)
{
//MessageBox.Show("An exception has been caught " + er1.ToString());
}

}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
finally
{
sftp.Disconnect();
}
}

I have looked in some of SSH.NET implementation, and I learned that I have to provide a downloadcallback argument and use a background thread, But this is only done for one file. Here what I have done.

private void button6_Click_1(object sender, EventArgs e)
{
Task.Run(() => Download());
}
private void Download()
{
try
{
filePath = @"D:\DataProfiler_Nautitech\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

//first data
string host = currentList[0].IPaddress;
string username = currentList[0].username;
string password = currentList[0].password;
string remoteDirectory = currentList[0].sourcefolder;
string localDirectory = currentList[0].destfolder;
string filextension = currentList[0].filextension;

using (var stream = new FileStream(SourcePath + FileName, FileMode.Create))
using (var client = new SftpClient(host, username, password))
{
client.Connect();
SftpFileAttributes attributes = client.GetAttributes(remoteDirectory+ 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);
}
}

I need filename to be the files from the directory. But I failed to do so.

I need to download the whole directory with the progress bar show the progression of the operation.

Is there is a way to embed this progress bar with this button.

Continue reading...
 
Back
Top