Registering event handlers |
The Sftp class may publish one or more events during the lifetime of an Sftp session. Any object that subscribes to events published by the Sftp class can receive and process these events.
Capturing the SftpConnectedEvent event
The SftpConnectedEvent is published by the Sftp instance once a connection has been established with the SSH server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpConnectedEvent += new Sftp.SftpConnectedEventHandler(OnConnected);
// define SftpConnectedEvent handler
public void OnConnected(object sender, SftpConnectedEventArgs e) {
Console.WriteLine("Connected to host {0}",e.Hostname);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' subscribe to event
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpConnectedEvent += New Sftp.SftpConnectedEventHandler(OnConnected)
' define SftpConnectedEvent handler
Public Sub OnConnected(ByVal sender As Object, ByVal e As SftpConnectedEventArgs)
Console.WriteLine("Connected to host {0}", e.Hostname)
End Sub
Capturing the SftpDisconnectedEvent event
The SftpDisconnectedEvent is published by the Sftp instance once the connection with the SSH server has been released.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpDisconnectedEvent += new Sftp.SftpDisconnectedEventHandler(OnDisconnected);
// define SftpDisconnectedEvent handler
public void OnDisconnected(object sender, SftpDisconnectedEventArgs e) {
Console.WriteLine("Disconnected from host {0}",e.Hostname);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Sftp instance
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpDisconnectedEvent += New Sftp.SftpDisconnectedEventHandler(OnDisconnected)
' define SftpDisconnectedEvent handler
Public Sub OnDisconnected(ByVal sender As Object, ByVal e As SftpDisconnectedEventArgs)
Console.WriteLine("Disconnected from host {0}", e.Hostname)
End Sub
Capturing the SftpDownloadEvent event
The SftpDownloadEvent is published by the Sftp instance after a file is sucessfully downloaded from the server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpDownloadEvent += new Sftp.SftpDownloadEventHandler(OnDownload);
// define SftpDownloadEvent handler
public void OnDownload(object sender, SftpDownloadEventArgs e) {
Console.WriteLine("Downloaded file {0}", e.File);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Sftp instance
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpDownloadEvent += New Sftp.SftpDownloadEventHandler(OnDownload)
' define SftpDownloadEvent handler
Public Sub OnDownload(ByVal sender As Object, ByVal e As SftpDownloadEventArgs)
Console.WriteLine("Downloaded file {0}", e.File)
End Sub
Capturing the SftpListingEvent event
The SftpListingEvent is published by the Sftp instance after a directory listing is performed.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpListingEvent += new Sftp.SftpListingEventHandler(OnListing);
// define SftpListingEvent listing handler
public void OnListing(object sender, SftpListingEventArgs e) {
Console.WriteLine("Listing retrieved");
IEnumerator files = e.Files;
while(files.MoveNext()) {
Jscape.Sftp.Packets.FileInfo file = (Jscape.Sftp.Packets.FileInfo)files.Current;
Console.WriteLine(file.ToString());
}
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Sftp instance
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpListingEvent += New Sftp.SftpListingEventHandler(OnListing)
' define SftpListingEvent handler
Public Sub OnListing(ByVal sender As Object, ByVal e As SftpListingEventArgs)
Console.WriteLine("Listing retrieved")
Dim files As IEnumerator = e.Files
While files.MoveNext
Dim file As Jscape.Sftp.Packets.FileInfo = CType(files.Current, Jscape.Sftp.Packets.FileInfo)
Console.WriteLine(file.ToString)
End While
End Sub
Capturing the SftpProgressEvent event
The SftpProgressEvent is published by the Sftp instance periodically duing the upload/download of files.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpProgressEvent += new Sftp.SftpProgressEventHandler(OnProgress);
// define SftpProgressEvent handler
public void OnProgress(object sender, SftpProgressEventArgs e) {
Console.WriteLine("Progress {0} of {1} bytes",e.Bytes,e.TotalBytes);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Sftp instance
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpProgressEvent += New Sftp.SftpProgressEventHandler(OnProgress)
' define SftpProgressEvent handler
Public Sub OnProgress(ByVal sender As Object, ByVal e As SftpProgressEventArgs)
Console.WriteLine("Progress {0} of {1} bytes", e.Bytes, e.TotalBytes)
End Sub
Capturing the SftpUploadEvent event
The SftpUploadEvent is published by the Sftp instance after a file is sucessfully uploaded to the server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Sftp instance
Sftp sftp = new Sftp(sshParams);
// subscribe to event
sftp.SftpUploadEvent += new Sftp.SftpUploadEventHandler(OnUpload);
// define SftpUploadEvent handler
public void OnUpload(object sender, SftpUploadEventArgs e) {
Console.WriteLine("Uploaded file {0}",e.File);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Sftp instance
Dim sftp As Sftp = New Sftp(sshParams)
' subscribe to event
sftp.SftpUploadEvent += New Sftp.SftpUploadEventHandler(OnUpload)
' define SftpUploadEvent handler
Public Sub OnUpload(ByVal sender As Object, ByVal e As SftpUploadEventArgs)
Console.WriteLine("Uploaded file {0}", e.File)
End Sub