EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Use FTPSBlackbox from Visual Basic .NET to upload and download files via FTP/FTPS (File Transfer Protocol)

FTPSBlackbox makes file operations via FTP/FTPS 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 FTP/FTPS 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 FTPSBlackbox an optimal choice, see Description of FTPSBlackbox.NET.

Download SecureBlackbox from the Download page.

Before you start

  • Check that you have an FTP/FTPS server to test.
  • 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 SBSimpleFTPS
  2. Imports SBX509
  3. Imports System.IO
  4.  
  5. Module ConsoleFTPSClient
  6.  
  7.     Dim WithEvents Client As TElSimpleFTPSClient
  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 = 21
  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 = 21
  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.         Dim stream As FileStream = New FileStream(parLocalName, FileMode.Create)
  83.         Try
  84.             Client.Receive(parRemoteName, stream)
  85.         Catch
  86.             stream.Close()
  87.         End Try
  88.     End Sub
  89.  
  90.     Sub Upload()
  91.         Dim stream As FileStream = New FileStream(parLocalName, FileMode.Open)
  92.         Try
  93.             Client.Send(Stream, parRemoteName)
  94.         Catch
  95.             stream.Close()
  96.         End Try
  97.     End Sub
  98.  
  99.     Sub List()
  100.         If Client.ExtMLSTSupported Then
  101.             Client.MLSD()
  102.         Else
  103.             Client.GetFileList()
  104.         End If
  105.     End Sub
  106.  
  107.     Private Sub m_Client_OnCertificateValidate(ByVal Sender As Object, ByVal Certificate As SBX509.TElX509Certificate, ByRef Validate As Boolean) Handles Client.OnCertificateValidate
  108.  
  109.         System.Console.WriteLine("Server certificate received")
  110.         Validate = True ' NEVER do this. You MUST check the key validity somehow
  111.     End Sub
  112.  
  113.     Private Sub m_Client_OnTextDataLine(ByVal Sender As Object, ByVal TextLine As Byte()) Handles Client.OnTextDataLine
  114.  
  115.         Dim response As String = System.Text.UTF8Encoding.UTF8.GetString(TextLine)
  116.         Dim FileInfo As TSBFTPFileInfo = New TSBFTPFileInfo
  117.  
  118.         Try
  119.  
  120.             If (Client.ExtMLSTSupported) Then
  121.                 TElSimpleFTPSClient.ParseMLSDEntry(response, FileInfo)
  122.             Else
  123.                 TElSimpleFTPSClient.ParseFileListEntry(response, FileInfo)
  124.             End If
  125.             System.Console.WriteLine(FileInfo.FileName)
  126.  
  127.         Catch
  128.             ' Couldn't parse the entry, so just write what we've received
  129.             System.Console.WriteLine(response)
  130.         End Try
  131.     End Sub
  132.  
  133.     Sub Main()
  134.  
  135.         SBUtils.Unit.SetLicenseKey("Your license key here")
  136.  
  137.         If Not ParseCommandLine() Then
  138.             Return
  139.         End If
  140.  
  141.         Client = New TElSimpleFTPSClient
  142.         Client.Address = parAddress
  143.         Client.Port = parPort
  144.         Client.Username = parUsername
  145.         Client.Password = parPassword
  146.    
  147.         Try
  148.             Try
  149.                 Client.Open()
  150.                 Client.Login()
  151.             Catch E As Exception
  152.                 System.Console.WriteLine("Connection failed due to exception: " + E.Message)
  153.                 System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,")
  154.                 System.Console.WriteLine("please contact EldoS support as described on https://www.eldos.com/sbb/support-tech.php")
  155.                 System.Console.WriteLine("Remember to provide details about the error that happened.")
  156.                 Try
  157.                     Client.Close(True)
  158.                 Catch
  159.                 End Try
  160.                 Return
  161.             End Try
  162.  
  163.             Try
  164.                 Select Case parOperation
  165.                     Case OP_DOWNLOAD
  166.                         Download()
  167.                         Exit Select
  168.                     Case OP_UPLOAD
  169.                         Upload()
  170.                         Exit Select
  171.                     Case OP_LIST
  172.                         List()
  173.                         Exit Select
  174.                 End Select
  175.             Finally
  176.                 Client.Close(False)
  177.             End Try
  178.  
  179.         Catch ex As Exception
  180.             System.Console.WriteLine(ex.Message)
  181.         End Try
  182.  
  183.     End Sub
  184.  
  185. End Module

Feel free to use this sample in your C# or VB.NET applications, that use FTP/FTPS. The console FTP/FTPS sample is available for C# and VB.NET. It's included into SecureBlackbox downloadable package.

FTPSBlackbox 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.

The components can be used on .NET and Mono server and desktop platforms, in ASP.NET and Silverlight applications, on smartphones that use Windows Phone or Android and on Windows RT tablets.

Read more about FTPSBlackbox .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!