ComponentPro UltimateSftp

      Using events

      Language Filter: AllSend comments on this topic to ComponentPro

      The Sftp class allows easy control file operations using events. This topic illustrates how to "wire-up" an event using Visual Studio environment and "wire-up" an event by yourself (if you are using the SFTP component as a class instead of adding it to the project as a component).

      Wiring-up an event using Visual Studio environment

      For C#

      1. Create a new C# project and add Ultimate SFTP component to your form. See Creating a WinForms application for information of how to do this.
      2. Click the component icon which was created on the panel underneath the form. Right-click on it and click on the Properties... menu.
      3. You should be represented with the Properties Window, click on the 'lightning bolt' icon to display all events for the SFTP component.
      4. Double-click the event you wish to "wire-up".
         
      5. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the HostKeyVerifying event as an example).
        void client_HostKeyVerifying(object sender, HostKeyVerifyingEventArgs e)
        {
            // Print out the fingerprint info. 
            Console.Write(string.Format("Host key: {0}. Do you want to accept this host key?(y/n):", e.HostKey.ToString()));
            string answer = Console.ReadLine();
            // Accept the fingerprint if user entered "y". 
            e.Accept = answer.ToLower() == "y";
        }
        
      6. Now you can connect to the SFTP server and check/display the host key in the HostKeyVerifying event handler.

      For VB.NET

      1. Create a new VB.NET project and add Ultimate SFTP component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to View then select Code to switch to code view.
      3. From the "drop-down" boxes at the top of the Code Window, select the instance of the SFTP component from the box on the left, then the name of the event you would like to "wire-up" from the box on the right.
      4. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the HostKeyVerifying event as an example).
         
        Private Sub client_HostKeyVerifying(ByVal sender As Object, ByVal e As HostKeyVerifyingEventArgs)
            ' Print out the fingerprint info. 
            Console.Write(String.Format("Host key: {0}. Do you want to accept this host key?(y/n):", e.HostKey.ToString()))
            Dim answer As String = Console.ReadLine()
            ' Accept the fingerprint if user entered "y". 
            e.Accept = answer.ToLower() = "y" 
        End Sub
        
      5. Now you can connect to the SFTP server and check/display the host key in the HostKeyVerifying event handler.

      Wiring-up an event as a class

      For C#

      1. Create a new C# project and add Ultimate SFTP component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the SFTP component such as:
        // Create a new class instance.
        Sftp client = new Sftp();
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up." For the purposes of this tutorial, the Sftp.HostKeyVerifying event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        client.HostKeyVerifying += client_HostKeyVerifying;
        
      5. Navigate to the reference listing for the HostKeyVerifying delegate. This listing will tell you what your event-handler method should be. According to the EventHandler<HostKeyVerifyingEventArgs> delegate, your method must accept two arguments (an object and a HostKeyVerifyingEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        void client_HostKeyVerifying(object sender, HostKeyVerifyingEventArgs e)
        {
            // Print out the fingerprint info. 
            Console.Write(string.Format("Host key: {0}. Do you want to accept this host key?(y/n):", e.HostKey.ToString()));
            string answer = Console.ReadLine();
            // Accept the fingerprint if user entered "y". 
            e.Accept = answer.ToLower() == "y";
        }
        
      6. Now you can connect to the SFTP server and check/display the host key in the HostKeyVerifying event handler.
        // Connect to the SFTP server.
        client.Connect("myserver");
        
        // Or you can specify the SFTP port with 
        // client.Connect("myserver", 22); 
         
        // Authenticate.
        client.Authenticate("userName", "password");
        
        // Do something here... 
        // ... 
         
        // Disconnect.
        client.Disconnect();
        

      For VB.NET

      1. Create a new VB.NET project and add Ultimate SFTP component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the SFTP component such as:
        ' Create a new class instance. 
        Dim client As New Sftp()
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up." For the purposes of this tutorial, the Sftp.HostKeyVerifying event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        AddHandler client.HostKeyVerifying, AddressOf client_HostKeyVerifying
        
      5. Navigate to the reference listing for the HostKeyVerifying delegate. This listing will tell you what your event-handler method should be. According to the EventHandler<HostKeyVerifyingEventArgs> delegate, your method must accept two arguments (an object and an HostKeyVerifyingEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        Private Sub client_HostKeyVerifying(ByVal sender As Object, ByVal e As HostKeyVerifyingEventArgs)
            ' Print out the fingerprint info. 
            Console.Write(String.Format("Host key: {0}. Do you want to accept this host key?(y/n):", e.HostKey.ToString()))
            Dim answer As String = Console.ReadLine()
            ' Accept the fingerprint if user entered "y". 
            e.Accept = answer.ToLower() = "y" 
        End Sub
        
      6. Now you can connect to the SFTP server and check/display the host key in the HostKeyVerifying event handler.
        ' Connect to the SFTP server.
        client.Connect("myserver")
        
        ' Or you can specify the SFTP port with 
        ' client.Connect("myserver", 22); 
         
        ' Authenticate.
        client.Authenticate("userName", "password")
        
        ' Do something here... 
        ' ... 
         
        ' Disconnect.
        client.Disconnect()