SFTP File Retrieval With .Net

By Beacon News | Published July 8, 2015 | Categories: Web Development

I am often tasked with developing .Net applications to interact with FTP servers for uploading and downloading files.  However, I was recently given a project involving file manipulation using SFTP (Secure File Transfer Protocol) and I was at a bit of a loss.  While there are endless online examples of using FTP with .Net, examples using SFTP were much scarcer.

Luckily for me, there is a wonderful SFTP client library specifically for .Net developed by Rebex:  http://www.rebex.net/sftp.net/.  Simply add the Rebex DLL file to your project, add a reference to it, and you’re ready to start accessing files from your application via SFTP.  I found it easier and cleaner to create a wrapper class to interact with the Rebex library.  From there it’s as simple as creating an instance of your wrapper class and downloading your files.  Complete code samples are below:

Example of using wrapper class to download file:

try
{
    bool success = false;
    SFTPDownloader sftp = new SFTPDownloader(SftpUrl, SftpUser, SftpPwd, SftpPath);
    if (sftp.DownloadFile(GetSFTPFileName(), GetZipFilePath())) success = true;
}
catch (Exception e)
{
    //Handle error
}

Wrapper class:

using System;
using System.Collections.Generic;
using System.Text;
using Rebex.Net;
 
namespace MyDotNetApp
{
    class SFTPDownloader
    {
        private bool _connectStatus = false;
        private string _url = "";
        private string _username = "";
        private string _password = "";
        private string _path = "";
        private string _error = "";
        private Sftp client;
 
        public bool ConnectStatus
        {
            get { return _connectStatus; }
        }
 
        public string Error
        {
            get { return _error; }
        }
 
        public SFTPDownloader(string URL, string UserName, string Password, string Path)
        {
            _url = URL;
            _username = UserName;
            _password = Password;
            _path = Path;
            _connectStatus = false;
        }
 
        private void Connect()
        {
            //Create client and connect
            _error = "";
            client = new Sftp();
            client.Connect(_url);
 
            //Authenticate
            client.Login(_username, _password);
 
            if (_path.Length > 0) client.ChangeDirectory(_path);
            _connectStatus = true;
        }
 
        public void Disconnect()
        {
            if (_connectStatus)
            {
                client.Disconnect();
                _connectStatus = false;
            }
        }
 
        public bool DownloadFile(string FileName, string localpath)
        {
            _error = "";
            bool tst = false;
 
            if (!_connectStatus) Connect();
 
            if (client.FileExists(FileName))
            {
                Int64 FileSize = client.GetFileLength(FileName);
                if (FileSize == client.GetFile(FileName, localpath)) tst = true;
            }
            else _error = "File Not found";
 
            return tst;
        }
 
        public DateTime GetFileTimeStamp(string FileName)
        {
            if (!_connectStatus) Connect();
            return client.GetFileDateTime(FileName);
        }
    }
}

Contact Us To Find Out More

Have questions? Need help? Contact us and we'll be in touch soon!

* Required

Beacon
164 Thatcher Road
Greensboro, NC 27409
Map and Directions

Sales & Marketing
336.232.5687