Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SFTP » Visual Basic 6

How to use SFTP in a Visual Basic project

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


The most important functions of the SFtp object are:


Step 1: Download and install the Network Component

Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual Basic project

Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':

Visual Basic

(Click on the picture to enlarge)

Step 3: Refer to the Network Component Library and create the objects

A new Project is created, with a blank form.

First, you must add a reference to Network Component in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Network Component 3.1 Type Library' reference as shown in the following picture:

(Click on the picture to enlarge)

Click 'OK' to close the 'References...' dialog.

Then, select the Project form and choose 'View Code' from the context menu:

(Click on the picture to enlarge)

On top of your code, declare the following object:

Public objSftp As AxNetwork.Sftp

Step 4: Create the object

From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:

Set objSftp = CreateObject("AxNetwork.Sftp")

Appedix: Full source code

Option Explicit
Private objSFtp As AxNetwork.SFtp
Private bConnected As Boolean

'///////////////////////////////////////////////////////////////////////

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260

Private Sub btnBrowse_Click()
  With dlgOpenfile
  .DefaultExt = "ppk"
  .DialogTitle = "Select ppl file"
  .InitDir = "C:\"
  .Filter = "Private key files |*.ppk"
  .FilterIndex = 1
  .ShowOpen
  End With
  
  txtKeyFile.Text = dlgOpenfile.FileName
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnConnect_Click()
  objSFtp.Clear
  objSFtp.LogFile = txtLogfile.Text
  objSFtp.Port = CInt(txtPort.Text)
  objSFtp.Host = txtHost.Text
  objSFtp.UserName = txtUsername.Text
  objSFtp.Password = txtPassword.Text
  objSFtp.PrivateKeyFile = txtKeyFile.Text
  objSFtp.AcceptHostKey = (cbAcceptKey = vbChecked)

  MousePointer = vbHourglass
  objSFtp.Connect

  If (GetResult = 0) Then
  
      bConnected = True
      RefreshDir
  End If

  UpdateControls
  
  MousePointer = vbDefault

End Sub

Private Sub btnDelete_Click()
  If (lsFiles.SelectedItem.Text <> "") Then
      objSFtp.DeleteFile (lsFiles.SelectedItem.Text)

      If (GetResult() = 0) Then
          RefreshDir
      End If
  End If
End Sub

Private Sub btnDisconnect_Click()
  objSFtp.Disconnect
  bConnected = False
  UpdateControls
End Sub

Private Sub btnGet_Click()
  If (lsFiles.SelectedItem.SubItems(1) <> "") Then

    dlgOpenfile.FileName = lsFiles.SelectedItem.Text
    dlgOpenfile.ShowSave
    
    objSFtp.GetFile lsFiles.SelectedItem.Text, dlgOpenfile.FileName

    If (GetResult() = 0) Then
        RefreshDir
    End If
  End If
End Sub

Private Sub btnPut_Click()
  dlgOpenfile.ShowOpen
  Dim target As String
  
  target = "./" + GetFileName(dlgOpenfile.FileName)
  objSFtp.PutFile dlgOpenfile.FileName, target
  
  If (GetResult() = 0) Then
      RefreshDir
  End If
End Sub

Private Sub btnRefresh_Click()
  RefreshDir
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub Form_Load()
  Set objSFtp = New AxNetwork.SFtp
  SetDefaultLogFile
  bConnected = False
  UpdateControls
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub UpdateControls()
  txtHost.Enabled = Not bConnected
  txtPort.Enabled = Not bConnected
  btnConnect.Enabled = Not bConnected
  txtUsername.Enabled = Not bConnected
  txtPassword.Enabled = Not bConnected
  btnDisconnect.Enabled = bConnected
  btnRefresh.Enabled = bConnected
  btnGet.Enabled = bConnected
  btnPut.Enabled = bConnected
  btnDelete.Enabled = bConnected
  lsFiles.Enabled = bConnected
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnView_Click()
  If FileExists(txtLogfile.Text) = True Then
    Shell "notepad " + txtLogfile.Text, vbNormalFocus
  End If
End Sub

'///////////////////////////////////////////////////////////////////////

Public Function FileExists(sFileName As String) As Boolean
  FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName))
End Function

'///////////////////////////////////////////////////////////////////////

Private Function SetDefaultLogFile()
  Dim Buffer As String
  Buffer = Space(MAX_PATH)
  
  If GetTempPath(MAX_PATH, Buffer) <> 0 Then
      txtLogfile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "SFtp.log"
  Else
      txtLogfile.Text = "C:\SFtp.log"
  End If
End Function

'///////////////////////////////////////////////////////////////////////

Private Function GetResult()
  GetResult = objSFtp.LastError
  txtResult.Text = objSFtp.LastError & ": " & objSFtp.GetErrorDescription(objSFtp.LastError)
End Function


'///////////////////////////////////////////////////////////////////////

Private Sub RefreshDir()
    
  Dim objItem As ListItem
  txtCwd.Text = objSFtp.GetCurrentDir

  lsFiles.ListItems.Clear
  
  Set objItem = lsFiles.ListItems.Add(, , "..")
  objItem.SubItems(1) = ""

  Dim objFile As SFtpFile

  Set objFile = objSFtp.FindFirstFile(".")

  While (objSFtp.LastError = 0)
      If Not (objFile.Name = "." Or objFile.Name = "..") Then
          Set objItem = lsFiles.ListItems.Add(, , objFile.Name)
  
          If (objFile.IsDirectory) Then
              objItem.SubItems(1) = ""
          Else
              objItem.SubItems(1) = objFile.SizeBytes
          End If
  
          objItem.SubItems(2) = objFile.Date
      End If
      Set objFile = objSFtp.FindNextFile
  Wend
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub lsFiles_DblClick()
  If (lsFiles.SelectedItem.SubItems(1) = "") Then
      objSFtp.ChangeDir (lsFiles.SelectedItem.Text)
      If (GetResult() = 0) Then
          RefreshDir
      End If
  End If
End Sub

Function GetFileName(ScanString As String) As String
  Dim intPos As Integer
  Dim intPosSave As Integer

  intPos = 1
  Do
      intPos = InStr(intPos, ScanString, "\")
      If intPos = 0 Then
          Exit Do
      Else
          intPos = intPos + 1
          intPosSave = intPos - 1
      End If
  Loop
  GetFileName = Trim$(Mid$(ScanString, intPosSave + 1))

End Function

You can download the complete sample on our FTP site ftp.activexperts-lab.com/samples/network-component/. There are many other working Network Component scripts on our site and shipped with the product.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The Network Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.