1

I'am using .NET 4.5 and I need to connect to an SFTP site and download two files to my local pc. From my reading on the internet there are no in built libraries I can use in .NET.

Are there any reliable 3rd parties that I can use that also have simple examples?

I have the following

username: myusername
password: mypassword
hostname: fts-sftp.myhost.com
protocol: SFTP
Port: 6621

Update

I have the code below however I am getting the following error message on the "sftp.Connect()" line.

An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

Additional information: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Have checked the creditenals that have been supplied to me to make sure I have no typo's.

using Renci.SshNet;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;

namespace SftpExample2
{
    class Program
    {
        static void Main(string[] args)
        {

            string host = "fts-sftp.myaddress.com";
            string password = "mypassword";
            string username = "myusername";
            string remoteDirectory = ".";
            int port = 6671;

            using (SftpClient sftp = new SftpClient(host, port, username, password))
            {
                sftp.Connect();

                var files = sftp.ListDirectory(remoteDirectory);

                foreach (var file in files)
                    Console.WriteLine(file.FullName);

                sftp.Disconnect();
             };

        }
     }
 }
mHelpMe
  • 6,336
  • 24
  • 75
  • 150

2 Answers2

1

I generaly use Renci.SshNet below is an example of download, it should be trivial to change it for download. I ripped it out of an old project, it might need some tuning to get it to compile/run

static public void UploadFiles(string [] files)
    {
        string host = " fts-sftp.myhost.com";
        string userName = "user";
        string password = "pass";

        var keyboardAuthMethod = new KeyboardInteractiveAuthenticationMethod(userName);
        keyboardAuthMethod.AuthenticationPrompt += delegate(Object senderObject, AuthenticationPromptEventArgs eventArgs)
        {
            foreach (var prompt in eventArgs.Prompts)
            {

                if (prompt.Request.Equals("Password: ", StringComparison.InvariantCultureIgnoreCase))
                {
                    prompt.Response = password;

                }

            }

        };

        var passwordAuthMethod = new PasswordAuthenticationMethod(userName, password);
        var connectInfo = new ConnectionInfo(host, userName, passwordAuthMethod, keyboardAuthMethod);

        using (SftpClient serverConnection = new SftpClient(connectInfo))
        {
            try
            {
foreach (var file in files)
{
    if (!file.Name.StartsWith("."))
    {
        string remoteFileName = file.Name;
        if (file.LastWriteTime.Date == DateTime.Today)

        Console.WriteLine(file.FullName);

        File.OpenWrite(localFileName);

        string sDir = @"localpath";

        Stream file1 = File.OpenRead(remoteDirectory + file.Name);
        sftp.DownloadFile(remoteDirectory, file1);
    }
                serverConnection.Disconnect();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

    }
Sibster
  • 3,081
  • 21
  • 18
  • thanks for your post. I've downloaded Renci.SshNet however have to be honest not sure what to do next with all the files & which ones I actually need to just download a file? – mHelpMe Mar 12 '15 at 08:57
  • 1
    simplest way is just to right click your solution in the solution explorer -> manage nuget packages -> make sure online => nuget.org is selected in the left pannel. then type ssh.Net in the search bar top right and hit enter. then just click install and all will be installed and configure for your project to use, just add using lines to your code – Sibster Mar 12 '15 at 09:17
  • great I now have it installed and configured in my project. Is it just the Renci.SshNet.Sftp that I need to use? – mHelpMe Mar 12 '15 at 09:24
  • 1
    Welcome to the magical world of nuget :) using Renci.SshNet; using Renci.SshNet.Common; – Sibster Mar 12 '15 at 09:25
0
    using Tamir.SharpSsh;
    public void DownloadSFTP_Files()
    {
        string _ftpURL = "URLHERE";
        string _SftpUserName = "USERNAMEHERE";
        string _SftpPassword = "PASSWORDHERE";
        int _port = 22;
        Sftp oSftp = new Sftp(_ftpURL, _SftpUserName, _SftpPassword);
        oSftp.Connect(_port);
        string path = "";
        // Get List of Files in the SFTP Directory
        System.Collections.ArrayList GetFiles_List = oSftp.GetFileList(path);
        // Download the Files Form SFTP Server to you Local system
        string ServerPath= "SERVERDiRECTORYPATHHERE";
        string LocalPath= "LOCALDiRECTORYPATHHERE";
        oSftp.Get(ServerPath, LocalPath);
        oSftp.Close();
    }
Shiva
  • 1
  • 3
    Add some explanation with answer for how this answer help OP in fixing current issue – ρяσѕρєя K Jan 16 '17 at 05:56
  • Try this code block to access Files in SFTP server.This is allow you to establish the connection to SFTP server and download the Files. – Shiva Jan 16 '17 at 12:12