If you want to use SFTP connection for file transfer, then there are several paid/license tools available in the market and some of them are very tough to work also. So, much complicated coding is required for simple file transfer using those tools.
Recently, one of my clients changed their file transfer server FTP to SFTP and then I did research for a while to find the best way to manage without any paid tool with simple/less C# code. I have used PSFTP with this example.
Now What is PSFTP?
Most of us know about PuTTY. PuTTY is a free SSH, Telnet and Rlogin client for 32-bit Windows systems. PSFTP, the PuTTY SFTP client, is a tool for transferring files securely between computers using an SSH connection. PuTTY link to download: http://www.putty.org/.
What You Need for SFTP?
- Host Name or FTP Server
- User Name
Example: Host Name or FTP Server: ftpdev.mycompany.com
User Name: ftptestuser
I will use this information in my coding, however you need to replace it with a valid one.
Let’s Start Now
Please follow the steps given below:
Step-1 (Installing PuTTY)
Download PuTTY from http://www.putty.org/ and install in your computer (it’s free). It will install at default path C:\Program Files (x86)\PuTTY.
Step-2 (authorizing PSFTP)
This is a mandatory step, you need to do since it will accept SSH certificate first time from SFTP server. So, this is required before C# coding is done.
Follow either option a or b:
- Option a: Click on, Windows start button -> All Programs -> PuTTY -> PSFTP
- Option b: Go to “C:\Program Files (x86)\PuTTY”, then double click on psftp.exe
You will see a window like:
Then try to connect manually.
Command 1: Open ftpdev.mycompany.com
Command 2: y
Command 3: Enter user name
Command 4: Enter password
Command 5: You can try some test commands to verify the SFTP files and folder list
Like Help;quit;ls;get;put etc. [more can be found at http://the.earth.li/~sgtatham/putty/0.52/htmldoc/Chapter6.html].
We need to install PuTTY (free tool) for developing this program, however you do not need any prior experience of PuTTY. You need to just follow the provided instructions.
Using the Code
Create a simple console app in Microsoft Visual Studio.
<add key ="psftp" value="C:\\Program Files (x86)\\PuTTY\\psftp.exe"/>
<add key ="user" value="ftptestuser"/>
<add key ="pass" value="ftptestpassword"/>
<add key ="ftpserver" value="ftpdev.mycompany.com"/>
static void Main(string args)
private static void PSFTP_TransferFile(string batchFile)
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = ConfigurationManager.AppSettings["psftp"].ToString();
var args1 = ConfigurationManager.AppSettings["user"].ToString() +
"@" + ConfigurationManager.AppSettings["ftpserver"].ToString() +
" -pw " + ConfigurationManager.AppSettings["pass"].ToString() +
" -batch -be -b " + AppDomain.CurrentDomain.BaseDirectory +
"PSFTP-BatchFile\\" + batchFile;
myProcess.StartInfo.Arguments = args1;
myProcess.StartInfo.CreateNoWindow = true;
var abc = myProcess.Start();
catch (Exception ex)
Console.WriteLine("Error occurred : " + ex.Message);
You can also download the complete solution from the link at the top of this article.
How to Configure from Task Scheduler?
Often, we use the SFTP in an automated environment for file transfer. You might find it difficult to run the above application from windows “Task Scheduler” if you will not configure it properly.
I will provide the steps for configuring in windows task scheduler. Please follow my instructions:
In Windows, go to start -> Control panel -> Administrative Tools -> Task Scheduler -> then right click on “Task Scheduler Library” -> Create Task
Step 1: General
Step 2: Triggers
Step 3: Actions
Points of Interest
This article will help you in building SFTP connectivity using PSFTP. You can add more features depending on your requirements.
Your feedback is always welcome.