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 Corporation 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
- Imports SBUtils
- Imports SBSftpCommon
- Imports SBSimpleSftp
-
- Module ConsoleSftpClient
-
- Dim Client As TElSimpleSFTPClient
-
- ' command line parameters
- Const OP_LIST As Integer = 1
- Const OP_DOWNLOAD As Integer = 2
- Const OP_UPLOAD As Integer = 3
-
- Dim parOperation As Integer = 0
- Dim parRemoteName As String = "/"
- Dim parLocalName As String
- Dim parUsername As String
- Dim parPassword As String
- Dim parAddress As String
- Dim parPort As Integer = 22
-
- Sub Download()
- ' Download the file from SFTP server and optionally overwrite local file if it exists
- Client.DownloadFile(parRemoteName, parLocalName, TSBSFTPFileTransferMode.ftmOverwrite)
- End Sub
-
- Sub Upload()
- ' Upload the file to SFTP server and optionally overwrite remote file if it exists
- Client.UploadFile(parLocalName, parRemoteName, TSBSFTPFileTransferMode.ftmOverwrite)
- End Sub
-
- Sub List()
- Dim Handle As Byte()
- Dim AListing As ArrayList
- ' Read the directory contents to AListing
- Handle = Client.OpenDirectory(parRemoteName)
- Try
- AListing = New ArrayList
- Client.ReadDirectory(Handle, AListing)
- Finally
- Client.CloseHandle(Handle)
- End Try
- ' List the contents
- If AListing.Count > 0 Then
- System.Console.WriteLine("Contents of " + parRemoteName + ":")
- Dim i As Integer
- For i = 0 To AListing.Count - 1
- ' we only write file names
- ' but it's possible to write other data such as date and attributes
- System.Console.WriteLine(CType(AListing(i), TElSftpFileInfo).Name)
- Next i
- Else
- System.Console.WriteLine("The specified directory is empty")
- End If
- End Sub
-
- Sub Main()
- ' set the license key. Trial license key is specified below.
- ' Trial key causes SecureBlackbox work slower.
- ' Request time-limited license key without delays on http://www.eldos.com/sbb/keyreq/
- SBUtils.Unit.SetLicenseKey("0190B807FC3C84EE4CAE3222EA8EBCC503B55E8ADEF669" _
- & "3234E36786EB2D9D4FC7AE8D79E5FAF39B757F24D91612EB46AB96F54B154B88E5DC3F1" _
- & "E17B9A1F4C9DC47ED662B1C088FD381213252B7633FFEC2D4D88DE70415BC9F89403D1D" _
- & "49A26E8279017526C44AFE15A6B2F707F60A984E25E7A8AFA6ABE01DFB9A7DC12A20B82" _
- & "ACB542145733EC394913AB6966604BA3ED0FEE2145667889094BC993B33E11648552AF6" _
- & "D8574D751BF22CE5BE648EFF26A3B0B785B95F0C5A8C239F567FAA19FB293E3EFAA0A7D1" _
- & "7D58EE9295841A0FD08EBCA3A74580C338F6F614833F3C00CD217E4B74977070AA6D3783" _
- & "1EA712542B7784481C178E0789F29BC749773A")
-
- ' Parse the command line parameters
- If Not ParseCommandLine() Then
- Return
- End If
-
- ' create SFTP client component
- Client = New TElSimpleSFTPClient
-
- ' set connection parameters
- Client.Address = parAddress
- Client.Port = parPort
- Client.Username = parUsername
- Client.Password = parPassword
-
- ' Perform the operation
- Try
- ' Connect to the SFTP server
- Client.Open()
- Try
- Select Case parOperation
- Case OP_DOWNLOAD
- Download()
- Exit Select
- Case OP_UPLOAD
- Upload()
- Exit Select
- Case OP_LIST
- List()
- Exit Select
- End Select
- Finally
- ' Disconnect from the SFTP server
- Client.Close(False)
- End Try
-
- Catch ex As Exception
- System.Console.WriteLine(ex.Message)
- End Try
-
- End Sub
-
- Function ParseCommandLine() As Boolean
-
- Dim Args As String()
- Args = Environment.GetCommandLineArgs
-
- Dim arg As String
-
- Dim i As Integer = 1
- If (Args.Length = 1) Then
- ParseCommandLine = False
- Exit Function
- End If
-
- While i < Args.Length
- arg = Args(i)
- Select Case arg
- Case "-down", "-download"
- parOperation = OP_DOWNLOAD
- Exit Select
- Case "-up", "-upload"
- parOperation = OP_UPLOAD
- Exit Select
- Case "-list"
- parOperation = OP_LIST
- Exit Select
- Case "-addr", "-address"
- i = i + 1
- parAddress = Args(i)
- Exit Select
- Case "-port"
- i = i + 1
- Try
- parPort = Int32.Parse(Args(i))
- Catch
- parPort = 22
- End Try
- Exit Select
- Case "-user", "-username"
- i = i + 1
- parUsername = Args(i)
- Exit Select
- Case "-pass", "-password"
- i = i + 1
- parPassword = Args(i)
- Exit Select
- Case "-local"
- i = i + 1
- parLocalName = Args(i)
- Exit Select
- Case "-remote"
- i = i + 1
- parRemoteName = Args(i)
- Exit Select
- End Select
- i = i + 1
- End While
- ParseCommandLine = parOperation <> 0
- End Function
-
- End Module
Feel free to use this sample in your C# or VB.NET applications, that use SFTP. The console SFTP sample is available for C# and VB.NET. It's included into SecureBlackbox 5.1 and is also available as a separate download.
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 (1.1, 2.0, 3.0, 3.5), .NET CF (Compact Framework) (1.0 or 2.0) or Mono (1.0 or 2.0) including Visual Studio 2008, Visual Studio 2005, Visual Studio .NET 2003, Borland Delphi 8, Delphi 2005, 2006 and 2007, Borland C# Builder
Read more about SFTPBlackbox .NET.
Download SecureBlackbox from the Download page.
