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 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
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
Sub Download()
Client.DownloadFile(parRemoteName, parLocalName, TSBSFTPFileTransferMode.ftmOverwrite)
End Sub
Sub Upload()
Client.UploadFile(parLocalName, parRemoteName, TSBSFTPFileTransferMode.ftmOverwrite)
End Sub
Sub List()
Dim Handle As Byte()
Dim AListing As ArrayList
Handle = Client.OpenDirectory(parRemoteName)
Try
AListing = New ArrayList
Client.ReadDirectory(Handle, AListing)
Finally
Client.CloseHandle(Handle)
End Try
If AListing.Count > 0 Then
System.Console.WriteLine("Contents of " + parRemoteName + ":")
Dim i As Integer
For i = 0 To AListing.Count - 1
System.Console.WriteLine(CType(AListing(i), TElSftpFileInfo).Name)
Next i
Else
System.Console.WriteLine("The specified directory is empty")
End If
End Sub
Private Sub m_Client_OnKeyValidate(ByVal Sender As Object, ByVal ServerKey As SBSSHKeyStorage.TElSSHKey, ByRef Validate As Boolean)
System.Console.WriteLine("Server key received")
Validate = True ' NEVER do this. You MUST check the key somehow
End Sub
Sub Main()
SBUtils.Unit.SetLicenseKey("Your license key here")
If Not ParseCommandLine() Then
Return
End If
Client = New TElSimpleSFTPClient
Client.Address = parAddress
Client.Port = parPort
Client.Username = parUsername
Client.Password = parPassword
AddHandler Client.OnKeyValidate, AddressOf m_Client_OnKeyValidate
Try
Try
Client.Open()
Catch E As Exception
System.Console.WriteLine("Connection failed due to exception: " + E.Message)
System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,")
System.Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php")
System.Console.WriteLine("Remember to provide details about the error that happened.")
If Client.ServerSoftwareName.Length > 0 Then
System.Console.WriteLine("Server software identified itself as: " + Client.ServerSoftwareName)
End If
Try
Client.Close(True)
Catch
End Try
Return
End Try
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
Client.Close(False)
End Try
Catch ex As Exception
System.Console.WriteLine(ex.Message)
End Try
End Sub
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 downloadable package.
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, 4.0), .NET CF (Compact Framework) (1.0, 2.0, 3.5), Silverlight or Mono (1.0 or 2.0) including Visual Studio 2010, Visual Studio 2008, Visual Studio 2005, Visual Studio .NET 2003, Delphi Prism and other .NET development environments.
Read more about SFTPBlackbox .NET.
Download SecureBlackbox.NET from the Download page.
|
Latest version
9.1.214
Released 30 January 2012
|