Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Secure FTP using C# and SFTP (FTP over SSH)

Overview

This article will demonstrate how to establish a secure connection and upload a file using the SFTP (FTP over SSH) protocol and the components provided in SSH Factory for .NET.

sftp .net downloadsftp .net purchase

Note: This example demonstrates using SFTP (FTP over SSH). If you are wanting to use the FTPS (FTP over SSL) protocol then please see the article Secure FTP using C# and FTPS (FTP over SSL).

Prerequisites

Example

   1:  using System;
   2:  using System.IO;
   3:  using Jscape.Ssh;
   4:  using Jscape.Sftp;
   5:   
   6:   
   7:  namespace SftpExample {
   8:      /// <summary>
   9:      /// Demonstrates connecting to an SSH server via SFTP and uploading a file.
  10:      /// </summary>
  11:      public class SftpUploadFile {
  12:   
  13:          public SftpUploadFile() {
  14:   
  15:              // get host and login information
  16:              Console.WriteLine("SFTP (FTP over SSH) example");
  17:              
  18:              Console.WriteLine("");
  19:   
  20:              Console.Write("Enter hostname: ");
  21:              string hostname = Console.ReadLine();
  22:              Console.Write("Enter username: ");
  23:              string username = Console.ReadLine();
  24:              Console.Write("Enter password: ");
  25:              string password = Console.ReadLine();
  26:   
  27:              // create login credentials
  28:              SshParameters ssh = new SshParameters(hostname,username,password);
  29:   
  30:              // create new Sftp instance
  31:              Sftp sftp = new Sftp(ssh);
  32:   
  33:              // register event handlers
  34:              sftp.SftpConnectedEvent += new Sftp.SftpConnectedEventHandler(OnConnected);
  35:              sftp.SftpDisconnectedEvent += new Sftp.SftpDisconnectedEventHandler(OnDisconnected);
  36:              sftp.SftpUploadEvent += new Sftp.SftpUploadEventHandler(OnUpload);
  37:              
  38:              // establish connection
  39:              Console.WriteLine("");
  40:              Console.WriteLine("Connecting to server...");
  41:              sftp.Connect();
  42:   
  43:              // prompt for file to upload
  44:              Console.Write("Enter file path to upload e.g. c:/tmp/file.txt: ");
  45:              string path = Console.ReadLine();        
  46:              FileInfo file = new FileInfo(path);
  47:   
  48:              // upload file
  49:              sftp.Upload(file);
  50:   
  51:              // disconnect
  52:              sftp.Disconnect();
  53:              Console.WriteLine("Done");
  54:          }
  55:   
  56:          /// <summary>
  57:          /// Invoked when connection is established.
  58:          /// </summary>
  59:          /// <param name="sender">the source of the event</param>
  60:          /// <param name="e">the event</param>
  61:          public void OnConnected(Object sender, SftpConnectedEventArgs e) {
  62:              Console.WriteLine("Connected to hostname " + e.Hostname);
  63:          }
  64:   
  65:          /// <summary>
  66:          /// Invoked when connection is released
  67:          /// </summary>
  68:          /// <param name="sender">the source of the event</param>
  69:          /// <param name="e">the event</param>
  70:          public void OnDisconnected(object sender, SftpDisconnectedEventArgs e) {
  71:              Console.WriteLine("Disconnected from hostname " + e.Hostname);
  72:          }
  73:   
  74:          /// <summary>
  75:          /// Invoked after file is uploaded.
  76:          /// </summary>
  77:          /// <param name="sender">the source of the event</param>
  78:          /// <param name="e">the event</param>
  79:          public void OnUpload(object sender, SftpUploadEventArgs e) {
  80:              Console.WriteLine("Uploaded file : " + e.File.ToString());
  81:          }        
  82:   
  83:          /// <summary>
  84:          /// The main entry point for the application.
  85:          /// </summary>                         
  86:          [STAThread]
  87:          static void Main(string[] args) {
  88:              SftpUploadFile upload = new SftpUploadFile();
  89:          }
  90:      }
  91:   
  92:   
  93:  }
  94:   
  95:   

  1. Lines 1-4. Add necessary using statements.
  2. Line 13. Constructor for example containing bulk of code.
  3. Lines 15-25. Prompt user for hostname and login credentials.
  4. Line 28. Create login credentials.
  5. Line 31. Create new Sftp instance passing in login credentials to constructor.
  6. Lines 33-36. Register event handlers for Sftp related events.
  7. Line 41. Establish connection.
  8. Line 44-46. Prompt user for file to upload.
  9. Line 49. Upload file.
  10. Line 52. Disconnect
  11. Lines 56-81. Event handler methods.
  12. Lines 86-89. Main method for running this example.

Summary

In this article you learned how to securely transfer files using the SFTP (FTP over SSH) protocol. The SFTP component in SSH Factory for .NET makes this easy removing the complexities of the SSH and SFTP protocols. To see what else SSH Factory for .NET has to offer Download a FREE 30 day SSH Factory for .NET Evaluation