EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Use SFTPBlackbox from Visual Basic .NET to upload and download files via SFTP (SSH File Transfer Protocol)

SFTP protocol is a part of SSH family of protocols. Nowadays SFTP has become a de-facto standard for secure transfer of files between Web servers. As .NET Framework doesn't include support for SSH and SFTP protocols, EldoS offers a powerful solution - SFTPBlackbox (.NET edition).

SFTPBlackbox makes file operations via SFTP on .NET platform as easy as 1-2-3. The code below is the complete console application, which can be used to upload and download files and list the directories. As you can see, the SFTP calls fit into just about a dozen of lines of code. The rest of code parses command-line parameters.

To see what outstanding features make SFTPBlackbox an optimal choice, see Description of SFTPBlackbox.NET or check the comparison chart of SFTP solutions for .NET

Download SecureBlackbox from the Download page.

Before you start

  • Check that you have an SSH server to test, and this SSH server has SFTP subsystem enabled. Alternatively, some FTP servers support SFTP protocol. If you don't have an SSH server...
  • Install SecureBlackbox .NET (you can download it from the Download page). If you plan to run the sample on the server, please read the deployment instructions in SecureBlackbox help file.

Source code

  1. Imports SBSftpCommon
  2.  
  3. Imports SBSimpleSftp
  4.  
  5. Module ConsoleSftpClient
  6.  
  7.     Dim Client As TElSimpleSFTPClient
  8.  
  9.     ' command line parameters
  10.     Const OP_LIST As Integer = 1
  11.     Const OP_DOWNLOAD As Integer = 2
  12.     Const OP_UPLOAD As Integer = 3
  13.  
  14.     Dim parOperation As Integer = 0
  15.     Dim parRemoteName As String = "/"
  16.     Dim parLocalName As String
  17.     Dim parUsername As String
  18.     Dim parPassword As String
  19.     Dim parAddress As String
  20.     Dim parPort As Integer = 22
  21.  
  22.     Function ParseCommandLine() As Boolean
  23.  
  24.         Dim Args As String()
  25.         Args = Environment.GetCommandLineArgs
  26.  
  27.         Dim arg As String
  28.  
  29.         Dim i As Integer = 1
  30.         If (Args.Length = 1) Then
  31.             ParseCommandLine = False
  32.             Exit Function
  33.         End If
  34.  
  35.         While i < Args.Length
  36.             arg = Args(i)
  37.             Select Case arg
  38.                 Case "-down", "-download"
  39.                     parOperation = OP_DOWNLOAD
  40.                     Exit Select
  41.                 Case "-up", "-upload"
  42.                     parOperation = OP_UPLOAD
  43.                     Exit Select
  44.                 Case "-list"
  45.                     parOperation = OP_LIST
  46.                     Exit Select
  47.                 Case "-addr", "-address"
  48.                     i = i + 1
  49.                     parAddress = Args(i)
  50.                     Exit Select
  51.                 Case "-port"
  52.                     i = i + 1
  53.                     Try
  54.                         parPort = Int32.Parse(Args(i))
  55.                     Catch
  56.                         parPort = 22
  57.                     End Try
  58.                     Exit Select
  59.                 Case "-user", "-username"
  60.                     i = i + 1
  61.                     parUsername = Args(i)
  62.                     Exit Select
  63.                 Case "-pass", "-password"
  64.                     i = i + 1
  65.                     parPassword = Args(i)
  66.                     Exit Select
  67.                 Case "-local"
  68.                     i = i + 1
  69.                     parLocalName = Args(i)
  70.                     Exit Select
  71.                 Case "-remote"
  72.                     i = i + 1
  73.                     parRemoteName = Args(i)
  74.                     Exit Select
  75.             End Select
  76.             i = i + 1
  77.         End While
  78.         ParseCommandLine = parOperation <> 0
  79.     End Function
  80.  
  81.     Sub Download()
  82.         Client.DownloadFile(parRemoteName, parLocalName, TSBSFTPFileTransferMode.ftmOverwrite)
  83.     End Sub
  84.  
  85.     Sub Upload()
  86.         Client.UploadFile(parLocalName, parRemoteName, TSBSFTPFileTransferMode.ftmOverwrite)
  87.     End Sub
  88.  
  89.     Sub List()
  90.         Dim Handle As Byte()
  91.         Dim AListing As ArrayList
  92.         Handle = Client.OpenDirectory(parRemoteName)
  93.         Try
  94.             AListing = New ArrayList
  95.             Client.ReadDirectory(Handle, AListing)
  96.         Finally
  97.             Client.CloseHandle(Handle)
  98.         End Try
  99.         If AListing.Count > 0 Then
  100.             System.Console.WriteLine("Contents of " + parRemoteName + ":")
  101.             Dim i As Integer
  102.             For i = 0 To AListing.Count - 1
  103.                 System.Console.WriteLine(CType(AListing(i), TElSftpFileInfo).Name)
  104.             Next i
  105.         Else
  106.             System.Console.WriteLine("The specified directory is empty")
  107.         End If
  108.     End Sub
  109.  
  110.     Private Sub m_Client_OnKeyValidate(ByVal Sender As Object, ByVal ServerKey As SBSSHKeyStorage.TElSSHKey, ByRef Validate As Boolean)
  111.         System.Console.WriteLine("Server key received")
  112.         Validate = True ' NEVER do this. You MUST check the key somehow
  113.     End Sub
  114.  
  115.     Sub Main()
  116.  
  117.         SBUtils.Unit.SetLicenseKey("Your license key here")
  118.  
  119.         If Not ParseCommandLine() Then
  120.             Return
  121.         End If
  122.  
  123.         Client = New TElSimpleSFTPClient
  124.         Client.Address = parAddress
  125.         Client.Port = parPort
  126.         Client.Username = parUsername
  127.         Client.Password = parPassword
  128.         AddHandler Client.OnKeyValidate, AddressOf m_Client_OnKeyValidate
  129.  
  130.         Try
  131.             Try
  132.                 Client.Open()
  133.             Catch E As Exception
  134.                 System.Console.WriteLine("Connection failed due to exception: " + E.Message)
  135.                 System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,")
  136.                 System.Console.WriteLine("please contact EldoS support as described on https://www.eldos.com/sbb/support-tech.php")
  137.                 System.Console.WriteLine("Remember to provide details about the error that happened.")
  138.                 If Client.ServerSoftwareName.Length > 0 Then
  139.                     System.Console.WriteLine("Server software identified itself as: " + Client.ServerSoftwareName)
  140.                 End If
  141.                 Try
  142.                     Client.Close(True)
  143.                 Catch
  144.                 End Try
  145.                 Return
  146.             End Try
  147.  
  148.             Try
  149.                 Select Case parOperation
  150.                     Case OP_DOWNLOAD
  151.                         Download()
  152.                         Exit Select
  153.                     Case OP_UPLOAD
  154.                         Upload()
  155.                         Exit Select
  156.                     Case OP_LIST
  157.                         List()
  158.                         Exit Select
  159.                 End Select
  160.             Finally
  161.                 Client.Close(False)
  162.             End Try
  163.  
  164.         Catch ex As Exception
  165.             System.Console.WriteLine(ex.Message)
  166.         End Try
  167.  
  168.     End Sub
  169.  
  170. End Module
  171.  

The console SFTP sample code is available for C# and VB.NET. It's included into SecureBlackbox downloadable package. Feel free to use this sample to have SFTP in VB.NET applications.

SFTPBlackbox can be used separately or in one cost-saving SecureBlackbox package. You can use SecureBlackbox with any .NET development tool that supports .NET Framework (4.6, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0), .NET for Windows RT, .NET CF (Compact Framework) (3.5, 2.0), Mono 4.0 or 2.0, Silverlight (5 or 4) and Xamarin.Android including Visual Studio 2015, 2013, 2012, 2010, 2008, 2005, MonoDevelop, Delphi Prism.

Read more about SFTPBlackbox .NET.

Download SecureBlackbox.NET from the Download page.

|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!