EldoS Corporation - Software components for data Security, Storage and Transfer
Support button
Home / SecureBlackbox® / SFTP .NET sample code for C# .NET - SecureBlackbox®
SITE SEARCH
Advanced search
SOLUTION GUIDE
For Software Developers
For Business Integrators
PRODUCT LINES
BizCrypto
SecureBlackbox
Callback File System
CallbackFilter
CallbackDisk
SolFS (Solid File System)
RawDisk
Rethync
MsgConnect
VoxPopuli
SFTP Net Drive
NEED HELP?
Support options
Knowledgebase
Forums
HelpDesk
CUSTOMER RELATIONS
Testimonials
Our clients
Geography
Contact Us
Time to Rest
My Control Center
COMPANY INFORMATION
Company news
Seasonal newsletter
Corporate information
For investors
For press
For partners
FOLLOW US

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

  1. using System;
  2. using System.Collections;
  3. using SBSSHCommon;
  4. using SBSSHKeyStorage;
  5. using SBSimpleSftp;
  6. using SBSftpCommon;
  7.  
  8. namespace ConsoleSftp
  9. {
  10.     /// <summary>
  11.     /// Summary description for ConsoleSftpClient.
  12.     /// </summary>
  13.     class ConsoleSftpClient
  14.     {
  15.  
  16.         static SBSimpleSftp.TElSimpleSFTPClient Client = null;
  17.  
  18.         // command line parameters
  19.         const int OP_LIST    = 1;
  20.         const int OP_DOWNLOAD = 2;
  21.         const int OP_UPLOAD  = 3;
  22.        
  23.         static long parOperation = 0;
  24.         static string parRemoteName = "/";
  25.         static string parLocalName;
  26.         static string parUsername;
  27.         static string parPassword;
  28.         static string parAddress;
  29.         static int parPort;
  30.    
  31.         static bool ParseCommandLine(string[] args)
  32.         {
  33.             int i = 0;
  34.             string arg;
  35.  
  36.             if (args.Length == 0)
  37.             {
  38.                 System.Console.WriteLine("usage:");
  39.                 System.Console.WriteLine("SFTPClient {-download|-upload} -addr[ess] <address> [-port <port>] -user[name] <user> -pass[word] <password> -remote <remote file name> -local <local file name>");
  40.                 return false;
  41.             }
  42.             else
  43.             {
  44.                 while (i < args.Length)
  45.                 {
  46.                     arg = args[i];
  47.                     if ((String.Compare(arg, "-down", true) == 0) ||
  48.                         (String.Compare(arg, "-download", true) == 0))
  49.                     {
  50.                         parOperation = OP_DOWNLOAD;
  51.                     }
  52.                     else
  53.                         if ((String.Compare(arg, "-up", true) == 0) ||
  54.                         (String.Compare(arg, "-upload", true) == 0))
  55.                     {
  56.                         parOperation = OP_UPLOAD;
  57.                     }
  58.                     else
  59.                         if ((String.Compare(arg, "-list", true) == 0) ||
  60.                         (String.Compare(arg, "-ls", true) == 0))
  61.                     {
  62.                         parOperation = OP_LIST;
  63.                     }
  64.                     else
  65.                         if ((String.Compare(arg, "-addr", true) == 0) ||
  66.                         (String.Compare(arg, "-address", true) == 0))
  67.                     {
  68.                         i++;
  69.                         parAddress = args[i];
  70.                     }
  71.                     else
  72.                         if (String.Compare(arg, "-port", true) == 0)
  73.                     {
  74.                         i++;
  75.                         try
  76.                         {
  77.                             parPort = Int32.Parse(args[i]);
  78.                         }
  79.                         catch (Exception)
  80.                         {
  81.                             parPort = 22;
  82.                         }
  83.                     }
  84.                     else
  85.                         if ((String.Compare(arg, "-user", true) == 0) ||
  86.                         (String.Compare(arg, "-username", true) == 0))
  87.                     {
  88.                         i++;
  89.                         parUsername = args[i];
  90.                     }
  91.                     else
  92.                         if ((String.Compare(arg, "-pass", true) == 0) ||
  93.                         (String.Compare(arg, "-password", true) == 0))
  94.                     {
  95.                         i++;
  96.                         parPassword = args[i];
  97.                     }
  98.                     else                   
  99.                         if (String.Compare(arg, "-local", true) == 0)
  100.                     {
  101.                         i++;
  102.                         parLocalName = args[i];
  103.                     }
  104.                     else                   
  105.                         if (String.Compare(arg, "-remote", true) == 0)
  106.                     {
  107.                         i++;
  108.                         parRemoteName = args[i];
  109.                     }
  110.                     i = i + 1;
  111.                 }
  112.                 return (parOperation != 0);
  113.             }
  114.         }
  115.  
  116.         static void Download()
  117.         {
  118.             Client.DownloadFile(parRemoteName, parLocalName, TSBSFTPFileTransferMode.ftmOverwrite);
  119.         }
  120.  
  121.         static void Upload()
  122.         {
  123.             Client.UploadFile(parLocalName, parRemoteName, TSBSFTPFileTransferMode.ftmOverwrite);
  124.            
  125.             // now adjust the attributes of the uploaded file
  126.             TElSftpFileAttributes Attrs = new TElSftpFileAttributes();
  127.             Attrs.CTime = DateTime.Now;
  128.             Attrs.MTime = Attrs.CTime;
  129.             Attrs.ATime = Attrs.CTime;
  130.             Attrs.CATime = Attrs.CTime;
  131.  
  132.             Attrs.IncludedAttributes = SBSftpCommon.__Global.saATime | SBSftpCommon.__Global.saMTime | SBSftpCommon.__Global.saCTime | SBSftpCommon.__Global.saCATime;
  133.             Client.SetAttributes(parRemoteName, Attrs);    
  134.            
  135.         }
  136.  
  137.         static void List()
  138.         {
  139.             byte[] Handle;
  140.             ArrayList AListing = new ArrayList();
  141.             Handle = Client.OpenDirectory(parRemoteName);
  142.             Client.ReadDirectory(Handle, AListing);
  143.             if (AListing.Count > 0)
  144.             {
  145.                 System.Console.WriteLine("Contents of " + parRemoteName + ":");
  146.                 for (int i = 0; i < AListing.Count; i++)
  147.                 {
  148.                     System.Console.WriteLine(((TElSftpFileInfo)AListing[i]).Name);
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 System.Console.WriteLine("The specified directory is empty");
  154.             }
  155.             Client.CloseHandle(Handle);
  156.         }
  157.  
  158.         static private void Client_OnKeyValidate(object Sender, TElSSHKey ServerKey, ref bool Validate)
  159.         {
  160.             System.Console.WriteLine("Server key received");
  161.             Validate = true; // NEVER do this. You MUST check the key validity somehow
  162.         }
  163.  
  164.         /// <summary>
  165.         /// The main entry point for the application.
  166.         /// </summary>
  167.         [STAThread]
  168.         static void Main(string[] args)
  169.         {
  170.  
  171.             SBUtils.Unit.SetLicenseKey("Your license key here");
  172.  
  173.             if (!ParseCommandLine(args))
  174.                 return;
  175.            
  176.             Client = new TElSimpleSFTPClient();
  177.             Client.Address = parAddress;
  178.             Client.Port = parPort;
  179.  
  180.             Client.Username = parUsername;
  181.             Client.Password = parPassword;
  182.  
  183.             Client.OnKeyValidate += new TSSHKeyValidateEvent(Client_OnKeyValidate);
  184.  
  185.             try
  186.             {
  187.                 try
  188.                 {
  189.                     Client.Open();
  190.                 }
  191.                 catch (Exception e)
  192.                 {
  193.                     System.Console.WriteLine("Connection failed due to exception: " + e.Message);
  194.                     System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,");
  195.                     System.Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php");
  196.                     System.Console.WriteLine("Remember to provide details about the error that happened.");
  197.                     if (Client.ServerSoftwareName.Length > 0)
  198.                     {
  199.                         System.Console.WriteLine("Server software identified itself as: " + Client.ServerSoftwareName);
  200.                     }
  201.                     try
  202.                     {
  203.                         Client.Close(true);
  204.                     }
  205.                     catch
  206.                     {
  207.                     }
  208.                     return;
  209.                 }
  210.  
  211.  
  212.                 try
  213.                 {
  214.                     switch (parOperation)
  215.                     {
  216.                         case OP_DOWNLOAD:
  217.                             Download();
  218.                             break;
  219.                         case OP_UPLOAD:
  220.                             Upload();
  221.                             break;
  222.                         case OP_LIST:
  223.                             List();
  224.                             break;
  225.                     }
  226.                 }
  227.                 finally
  228.                 {
  229.                     Client.Close(false);
  230.                 }
  231.             }
  232.             catch (Exception e)
  233.             {
  234.                 System.Console.WriteLine("Exception: " + e.Message);
  235.             }
  236.         }
  237.     }
  238. }
  239.  

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 (4.5, 4.0, 3.5, 3.0, 2.0), .NET for Windows RT, .NET CF (Compact Framework) (3.5, 2.0), Mono 2.0, Silverlight (5 or 4) and Mono for Android including Visual Studio 2012, 2010, 2008, 2005, MonoDevelop, Delphi Prism.

Read more about SFTPBlackbox .NET.

Download SecureBlackbox.NET from the Download page.

|
Most wanted features
Vote or request a feature
Support and Resources
  • Knowledgebase & FAQ
  • Documentation on-line
  • Forum
  • Ask a question in HelpDesk
Latest version

Release:  10.0.233
28 February 2013

  • New And Improved Features
  • Change list
  • Download
Contact Us | Terms of Use | Trademarks | Privacy Statement | Site Index
Copyright (c) 1998-2013, EldoS Corporation
Design by Web Arsenal