Windows Forms Put Code Example
The following example demonstrates an SFTP session in a Windows Forms environment, in which a file is uploaded to the server. The session is executed asynchronously, without blocking the UI. The session is logged and progress is provided during the transfer.
private void butPut_Click(object sender, System.EventArgs e)
//This application has a User-Interface, which we do not want to block.
//Start launches the specified method on a worker thread.
//Event handlers execute on the UI thread.
private void putFile(object state)
//This function performs an SFTP Put operation using blocking calls.
//Use the Start method to execute this function on a worker thread.
//Connect and authenticate.
sftp1.Connection.RemoteEndPoint.HostNameOrAddress = "mySFtpServer";
//Put the file and marshal the result back to the UI thread via the UserState event.
CopyResult result = sftp1.Put("c:\\MyFiles\\myFile.pdf", "myFile.pdf", CopyMode.Copy);
catch (Exception ex)
//Marshal exceptions to the UI thread via the Error event.
//Logout and close the connection.
private void sftp1_Progress(object sender, ProgressEventArgs e)
//Update a progress bar using information from the Progress event.
progressBar.Value = Convert.ToInt32(e.Progress.ByteCount * 100 / e.Progress.Size);
private void sftp1_Debug(object sender, DebugEventArgs e)
//Update a log with communication between the application and the server.
private void sftp1_UserState(object sender, UserStateEventArgs e)
//This event is raised when a CopyResult is marshaled.
textLog.AppendText("Operation completed: " +
((CopyResult)e.UserState).Status.ToString() + "\r\n");
private void sftp1_Error(object sender, ComponentErrorEventArgs e)
//Update the log with any exceptions that occur.
textLog.AppendText("ERROR: " + e.GetException().Message + "\r\n");