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.
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).
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: