SFTP Progress Monitoring and Abort (C#)
Here is an example for monitoring the progress of an SFTP file transfer:
void sftp_OnPercentDone(object sender, Chilkat.PercentDoneEventArgs args)
{
progressBar1.Value = args.PercentDone;
// To abort at any point, you may set args.Abort = true
// args.Abort = true;
}
void sftp_OnAbortCheck(object sender, Chilkat.AbortCheckEventArgs args)
{
// See https://cknotes.com/?p=149 for more information about
// using the AbortCheck event...
}
private void button2_Click(object sender, EventArgs e)
{
// SFTP upload with progress monitoring.
Chilkat.SFtp sftp = new Chilkat.SFtp();
// IMPORTANT: Enable event callbacks:
sftp.EnableEvents = true;
// Unlock the component.
bool success = sftp.UnlockComponent("Anything for 30-day trial");
if (!success)
{
textBox1.Text = sftp.LastErrorText;
return;
}
// Connect to the SSH/SFTP server. (use a domain name or IP address)
success = sftp.Connect("192.168.1.117", 22);
if (!success)
{
textBox1.Text = sftp.LastErrorText;
return;
}
// Authenticate..
success = sftp.AuthenticatePw("chilkat", "myPassword");
if (!success)
{
textBox1.Text = sftp.LastErrorText;
return;
}
success = sftp.InitializeSftp();
if (!success)
{
textBox1.Text = sftp.LastErrorText;
return;
}
// Establish some event handlers:
sftp.OnAbortCheck += new Chilkat.SFtp.AbortCheckEventHandler(sftp_OnAbortCheck);
sftp.OnPercentDone += new Chilkat.SFtp.PercentDoneEventHandler(sftp_OnPercentDone);
// See https://cknotes.com/?p=149 for more information about
// using the AbortCheck event...
// Do an upload.
// Note: If the file to be uploaded or downloaded is too small,
// the PercentDone callback will not be called for each 1%.
// It's possible if the file is so small relative to the speed
// of the connection that you may get a single 100% callback if
// the data transfer happens almost instantaneously.
string localFilepath = "c:/temp/hamlet.xml";
string remoteFilepath = "hamlet.xml";
progressBar1.Value = 0;
success = sftp.UploadFileByName(remoteFilepath, localFilepath);
if (!success)
{
textBox1.Text = sftp.LastErrorText;
return;
}
MessageBox.Show("OK!");
}
admin
0
Tags :