Click here to Skip to main content
13,146,315 members (43,861 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.7K views
148 downloads
9 bookmarked
Posted 8 Mar 2017

Secure FTP or SFTP using Putty/PSFTP with C# (Open Source) and Task Scheduler Configuration

, 9 Mar 2017
Rate this:
Please Sign up or sign in to vote.
Simple open source code and technique for SFTP connectivity

Introduction

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?

  1. Host Name or FTP Server
  2. User Name
  3. Password

Example: Host Name or FTP Server: ftpdev.mycompany.com
User Name:
ftptestuser
Password:
ftptestpassword

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].

Background

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.

App.config:

<appSettings>
  <!--psftp path-->
  <add key ="psftp"  value="C:\\Program Files (x86)\\PuTTY\\psftp.exe"/>

  <!--ftp server and credentials-->
  <add key ="user"  value="ftptestuser"/>
  <add key ="pass"  value="ftptestpassword"/>
  <add key ="ftpserver"  value="ftpdev.mycompany.com"/>
</appSettings>

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace PSFTP_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Example of SFTP using PSFTP */

            /* in this example the local files are placed in 
            bin/debug folder since i am using AppDomain.CurrentDomain.BaseDirectory path
            you can change it but make sure your run time code is accessible to that path.
            */

            /*Send file from local folder to FTP server 
             send.sftp is a batch file, look the command in the file from the solution            
             */
            PSFTP_TransferFile("send.sftp");
            /*Receive file from FTP server to local folder
             recieve.sftp is a batch file, look the command in the file from the solution            
             */
            PSFTP_TransferFile("recieve.sftp");
            /*delete file from FTP server
             delete.sftp is a batch file, look the command in the file from the solution            
             */
            PSFTP_TransferFile("delete.sftp");
        }
        private static void PSFTP_TransferFile(string batchFile)
        {
            try
            {
                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();
                myProcess.WaitForExit();
            }
            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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manas Kumar Mohanty
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionSsh.net ? Pin
Cestbienmoi10-Mar-17 2:55
memberCestbienmoi10-Mar-17 2:55 
AnswerRe: Ssh.net ? Pin
Manas Kumar Mohanty11-Mar-17 16:10
memberManas Kumar Mohanty11-Mar-17 16:10 
QuestionImages are broken Pin
rht3419-Mar-17 3:20
memberrht3419-Mar-17 3:20 
AnswerRe: Images are broken Pin
Manas Kumar Mohanty9-Mar-17 4:53
memberManas Kumar Mohanty9-Mar-17 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 9 Mar 2017
Article Copyright 2017 by Manas Kumar Mohanty
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid