How to code your own sFTP File Uploader Windows application using C# and SSH

SFTP stands for SSH File Transfer Protocol. SSH is an encrypted and secure communication protocol, and it provides an extension to transfer files. In fact, SFTP is completely different from FTP. It still does essentially the same job, but securely, and with better compatibility and formality than FTP.

Step 1 Open Visual Studio IDE and start a new windows form application.

Step 2 – Add labels for host, username, password, port, directory and file. Also add two buttons and label one of them browse and the other start. These buttons will be used to load the file and the other to start the upload.

 

Step 3 – In Visual Studio go to Tools-> NuGet Package Manager-> Manage NuGet Packages for Solution.

 

 

 

 

 

Step 4 – Look up SSH.NET by Renci and Install it. This will install the library needed for SSH.

 

 

 

 

 

Below is the code that you need to add in the rest of your project.

sFTP Windows Form Client Application
Windows sFTP Client Form
Visual Studio NuGet Package Manager
Visual Studio NuGet Package Manager
Install Rencii SSH
NuGet Package Manager Rencii SSH

In your Program.cs code Add:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sFTP
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
}
}

In sftp.cs add:

using Renci.SshNet;
using System.IO;

namespace sFTP
{
class sftp
{

public static void UploadSFTPFile(string host, string username, string password, string sourcefile, string destinationpath, int port)
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(destinationpath);
using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(sourcefile));
}
}

}
}
}

 

And in Main.cs add:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace sFTP
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}

struct FtpSetting
{

public string Host{ get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Filename { get; set; }
public string Fullname { get; set; }
public string Port { get; set; }

}

FtpSetting _inputParameter;

private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, ValidateNames = true, Filter = "All files|*.*" })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo fi = new FileInfo(ofd.FileName);
_inputParameter.Username = txtUsername.Text;
_inputParameter.Password = txtPassword.Text;
_inputParameter.Host = txtHost.Text;
_inputParameter.Port = txtPort.Text;
_inputParameter.Filename = fi.Name;
_inputParameter.Fullname = fi.FullName;
txtFilename.Text = fi.FullName;
}
}
}

private void btnStart_Click(object sender, EventArgs e)
{
// string filename = ((FtpSetting)e.Argument).Filename;
var host = txtHost.Text;
var username = txtUsername.Text;
var password = txtPassword.Text;
var source = txtFilename.Text;
var destination = txtDestination.Text;
var port = Int32.Parse(txtPort.Text);
sftp.UploadSFTPFile(host, username, password, source, destination, port);
}

}
}

 

Leave a Reply


ArcMap 10.4 – Using python function in field calculator to update date values from another column

Total Page Visits: 396 – Today Page Visits: 3

Loading Data from Multiple Tables into Data Grid using Entity Framework

Total Page Visits: 396 – Today Page Visits: 3

XLS to DBF File Conversion using ExcelDataReader

Total Page Visits: 396 – Today Page Visits: 3

Total Page Visits: 396 - Today Page Visits: 3