Registering event handlers |
The Scp class may publish one or more events during the lifetime of an Scp session. Any object that subscribes to events published by the Scp class can receive and process these events.
Capturing the ScpConnectedEvent event
The ScpConnectedEvent is published by the Scp 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 Scp instance
Scp scp = new Scp(sshParams);
// subscribe to event
scp.ScpConnectedEvent += new Scp.ScpConnectedEventHandler(OnConnected);
// define ScpConnectedEvent handler
public void OnConnected(object sender, ScpConnectedEventArgs 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 scp As Scp = New Scp(sshParams)
' subscribe to event
scp.ScpConnectedEvent += New Scp.ScpConnectedEventHandler(OnConnected)
' define ScpConnectedEvent handler
Public Sub OnConnected(ByVal sender As Object, ByVal e As ScpConnectedEventArgs)
Console.WriteLine("Connected to host {0}", e.Hostname)
End Sub
Capturing the ScpDisconnectedEvent event
The ScpDisconnectedEvent is published by the Scp 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 Scp instance
Scp scp = new Scp(sshParams);
// subscribe to event
scp.ScpDisconnectedEvent += new Scp.ScpDisconnectedEventHandler(OnDisconnected);
// define ScpDisconnectedEvent handler
public void OnDisconnected(object sender, ScpDisconnectedEventArgs e) {
Console.WriteLine("Disconnected from host {0}",e.Hostname);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Scp instance
Dim scp As Scp = New Scp(sshParams)
' subscribe to event
scp.ScpDisconnectedEvent += New Scp.ScpDisconnectedEventHandler(OnDisconnected)
' define ScpDisconnectedEvent handler
Public Sub OnDisconnected(ByVal sender As Object, ByVal e As ScpDisconnectedEventArgs)
Console.WriteLine("Disconnected from host {0}", e.Hostname)
End Sub
Capturing the ScpDownloadEvent event
The ScpDownloadEvent is published by the Scp instance after a file is sucessfully downloaded from the server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Scp instance
Scp scp = new Scp(sshParams);
// subscribe to event
scp.ScpDownloadEvent += new Scp.ScpDownloadEventHandler(OnDownload);
// define ScpDownloadEvent handler
public void OnDownload(object sender, ScpDownloadEventArgs e) {
Console.WriteLine("Downloaded file {0}", e.Filename);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Scp instance
Dim Scp As Scp = New Scp(sshParams)
' subscribe to event
Scp.ScpDownloadEvent += New Scp.ScpDownloadEventHandler(OnDownload)
' define ScpDownloadEvent handler
Public Sub OnDownload(ByVal sender As Object, ByVal e As ScpDownloadEventArgs)
Console.WriteLine("Downloaded file {0}", e.Filename)
End Sub
Capturing the ScpProgressEvent event
The ScpProgressEvent is published by the Scp instance periodically duing the upload/download of files.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Scp instance
Scp Scp = new Scp(sshParams);
// subscribe to event
Scp.ScpProgressEvent += new Scp.ScpProgressEventHandler(OnProgress);
// define ScpProgressEvent handler
public void OnProgress(object sender, ScpProgressEventArgs 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 Scp instance
Dim Scp As Scp = New Scp(sshParams)
' subscribe to event
Scp.ScpProgressEvent += New Scp.ScpProgressEventHandler(OnProgress)
' define ScpProgressEvent handler
Public Sub OnProgress(ByVal sender As Object, ByVal e As ScpProgressEventArgs)
Console.WriteLine("Progress {0} of {1} bytes", e.Bytes, e.TotalBytes)
End Sub
Capturing the ScpUploadEvent event
The ScpUploadEvent is published by the Scp instance after a file is sucessfully uploaded to the server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Scp instance
Scp Scp = new Scp(sshParams);
// subscribe to event
Scp.ScpUploadEvent += new Scp.ScpUploadEventHandler(OnUpload);
// define ScpUploadEvent handler
public void OnUpload(object sender, ScpUploadEventArgs e) {
Console.WriteLine("Uploaded file {0}",e.Filename);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Scp instance
Dim Scp As Scp = New Scp(sshParams)
' subscribe to event
Scp.ScpUploadEvent += New Scp.ScpUploadEventHandler(OnUpload)
' define ScpUploadEvent handler
Public Sub OnUpload(ByVal sender As Object, ByVal e As ScpUploadEventArgs)
Console.WriteLine("Uploaded file {0}", e.Filename)
End Sub