none
SFTP download using C#

    Question

  • Hi Guys,

    I have a very weird SFTP Server. This belongs to a client of mine. We are in need of downloading the files from it.

    The files they post everyday has the same name. To make it even difficult the time-stamp also doesn't change(given below)

    

    The folder hierarchy is also vast (4 primary folder, each folder has A-K serial ranged folder inside which files will be posted)

    They will post the files in these folder the file time stamp will also be the same.

    That narrows our option of identifying the new file only based on file size difference.

    I have been asked to write a piece of code in C# for this, to recursively check the Folder hierarchy and get the files that are varying size with previous day.

    I am new to C#.  I saw a code in the forum for C# SFTP

    Please assist me in writing a code for my requirement.

    Thanks,

    Sarathi.

    Wednesday, October 23, 2013 9:18 AM

Answers

  • Hi Sarathi,

    .NET Framework does not support SFTP natively. You may need to use a third party or open source component to implement it.

    Take a look at the following links.

    SharpSSH is open source and a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.

    Granados is also an SSH client library for .NET.

    Rebex SFTP is a versatile file-transfer component for .NET languages (such as C# or VB.NET) that provides secure file system access over an SSH channel using the SFTP protocol.

    Thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 24, 2013 6:23 AM
    Moderator
  •         /// <summary>
            /// This sample will download a file on the remote system to your local machine.
            /// </summary>
            public void DownloadFile()
            {
                string host = "";
                string username = "";
                string password = "";
                string localFileName = System.IO.Path.GetFileName(localFile);
                string remoteFileName = "";
    
                using (var sftp = new SftpClient(host, username, password))
                {
                    sftp.Connect();
    
                    using (var file = File.OpenWrite(localFileName))
                    {
                        sftp.DownloadFile(remoteFileName, file);
                    }
    
                    sftp.Disconnect();
                }
            }
    
            /// <summary>
            /// This will list the contents of the current directory.
            /// </summary>
            public void ListDirectory()
            {
                string host = "";
                string username = "";
                string password = "";
                string remoteDirectory = "."; // . always refers to the current directory.
    
                using (var sftp = new SftpClient(host, username, password))
                {
                    sftp.Connect();
                    var files = sftp.ListDirectory(remoteDirectory);
                    foreach (var file in files)
                    {
                        Console.WriteLine(file.FullName);
                    }
                    sftp.Disconnect();
                }
            }
    

    Here is the code you need if you use SSH.NET.

    https://sshnet.codeplex.com/

    SSH.NET is also avaliable via NuGet as well.

    http://www.nuget.org/packages/SSH.NET/

    Thursday, October 24, 2013 2:07 PM

All replies

  • Hi Sarathi,

    .NET Framework does not support SFTP natively. You may need to use a third party or open source component to implement it.

    Take a look at the following links.

    SharpSSH is open source and a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.

    Granados is also an SSH client library for .NET.

    Rebex SFTP is a versatile file-transfer component for .NET languages (such as C# or VB.NET) that provides secure file system access over an SSH channel using the SFTP protocol.

    Thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 24, 2013 6:23 AM
    Moderator
  • This should be almost everything.

    http://www.dart.com/sftp-ssh-code-examples-samples.aspx

    chanmm


    chanmm

    Thursday, October 24, 2013 9:17 AM
  •         /// <summary>
            /// This sample will download a file on the remote system to your local machine.
            /// </summary>
            public void DownloadFile()
            {
                string host = "";
                string username = "";
                string password = "";
                string localFileName = System.IO.Path.GetFileName(localFile);
                string remoteFileName = "";
    
                using (var sftp = new SftpClient(host, username, password))
                {
                    sftp.Connect();
    
                    using (var file = File.OpenWrite(localFileName))
                    {
                        sftp.DownloadFile(remoteFileName, file);
                    }
    
                    sftp.Disconnect();
                }
            }
    
            /// <summary>
            /// This will list the contents of the current directory.
            /// </summary>
            public void ListDirectory()
            {
                string host = "";
                string username = "";
                string password = "";
                string remoteDirectory = "."; // . always refers to the current directory.
    
                using (var sftp = new SftpClient(host, username, password))
                {
                    sftp.Connect();
                    var files = sftp.ListDirectory(remoteDirectory);
                    foreach (var file in files)
                    {
                        Console.WriteLine(file.FullName);
                    }
                    sftp.Disconnect();
                }
            }
    

    Here is the code you need if you use SSH.NET.

    https://sshnet.codeplex.com/

    SSH.NET is also avaliable via NuGet as well.

    http://www.nuget.org/packages/SSH.NET/

    Thursday, October 24, 2013 2:07 PM
  • @IdahoSixString,

    For the code you have written may i know what SSH you have used

    I am getting the following error

    Error 1 The type or namespace name 'SftpClient' could not be found (are you missing a using directive or an assembly reference?) C:\Users\sarathik\Desktop\SFTP Test\SFTP Test\SFTP Test\Program.cs 18 35 SFTP Test

    What component should i need to install to get this namespace working.

    I am new to .Net Please help.

    Thursday, November 07, 2013 11:51 AM
  • Hi Sarathi Kamaraj,

    The message represents that you are missing a reference to the assembly. Try the below code to add the namespace.

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

    You must add some references about Renci.SshNet.Common and Renci.SshNet.Sftp.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 08, 2013 1:49 AM
    Moderator
  • using Renci.SshNet.Common;
    using Renci.SshNet.Sftp;

    You will need both of these.

    A simple trick to help you make sure you have all your using statements is if you see this problem again go ahead and press CLTRL + . and it will bring up a menu and you can add your using statements this way.

    Thursday, November 14, 2013 3:48 PM
  • I have tried the above sample. It works for first run. However, subsequently it throw the exception below

    "The process cannot access the file 'C:\test.txt' because it is being used by another process."

    Alternatively, i did tried to open the file like below but still no luck.

    using (FileStream file= new FileStream(localFileName, FileMode.Open, FileAccess.Write,FileShare.None))
    {
       client.DownloadFile(remoteFile, file, null);
    }

    Please help.

    Carolyn



    • Edited by meowling Wednesday, March 26, 2014 2:30 AM
    Wednesday, March 26, 2014 2:29 AM