1

I need to know a way to connect to a FTP site through SFTP. I am using SharpSSH and i am unable to find an example to do the program.

For now, i have downloaded the SharpSSH .DLL files and added as references. Now i need to write the code where i could connect, and upload/download files from the FTP server.

How can i do this ? Help.

UPDATE enter image description here

Code :

//ip of the local machine and the username and password along with the file to be uploaded via SFTP.
 FileUploadUsingSftp("http://Some-sftp-site.com", "username", "password", @"D:\", @"name.txt");

The above code is in the Main Method.

Then ;

private static void FileUploadUsingSftp(string FtpAddress, string FtpUserName, string FtpPassword, string FilePath, string FileName)
        {
            Sftp sftp = null;
            try
            {
                // Create instance for Sftp to upload given files using given credentials
                sftp = new Sftp(FtpAddress, FtpUserName, FtpPassword);

                // Connect Sftp
                sftp.Connect();

                // Upload a file
                sftp.Put(FilePath + FileName);

                // Close the Sftp connection
                sftp.Close();
            }
            finally
            {
                if (sftp != null)
                {
                    sftp.Close();
                }
            }
        }
Illep
  • 16,375
  • 46
  • 171
  • 302
  • 2
    Is it an FTP site, or an SFTP site, or a FTPS site? They are 3 different things. – Joe Jun 05 '12 at 14:41
  • This appears to be a problem with DNS resolution. Can your machine successfully resolve the name of your SFTP server to an IP address? – Joe Jun 05 '12 at 15:41
  • Are you changing the username and password to the FTP's username and password. – Botonomous Jun 05 '12 at 15:51
  • Can someone tell me what i should pass for `string FilePath, string FileName` ? Is `FilePath` the path to be found in the SFTP server ? and `FileName` is the file that will be saved in the server ? – Illep Jun 05 '12 at 16:16
  • Use this - it is GREAT! http://stackoverflow.com/a/5423005/966609 – Matt Canty Jul 12 '12 at 11:12
  • Follow C# code snippet that uses Tamir.SharpSSH.dll to assist in uploading file over SFTP. [SFTP – Upload file](http://technowide.net/2012/09/21/sftp-upload-file/) – Sachin Dhir Dec 30 '14 at 08:32

2 Answers2

2

What have you done as of right now?

We can't just give you a straight answer on 'how to upload files'....

Here is a tutorial: http://saravanandorai.blogspot.com/2012/01/sftp-and-file-upload-in-sftp-using-c.html

Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • First i would need to establish a connection to the SFTP server. and then download the files to a local directory – Illep Jun 05 '12 at 14:50
  • Take a look at the tutorial I provided. It has everything your asking for. – Botonomous Jun 05 '12 at 14:56
  • I have updated my question. I get an exception when i do it according to the tutorial. Can you help me solve this ? – Illep Jun 05 '12 at 15:06
  • Can someone tell me what i should pass for `string FilePath, string FileName` ? Is `FilePath` the path to be found in the SFTP server ? and `FileName` is the file that will be saved in the server ? – Illep Jun 05 '12 at 16:13
  • If you look in the method the comments clearly show the FilePath and FileName are concated together which represent the full pathname to the location of the file you are wanting to upload. – Botonomous Jun 05 '12 at 16:49
  • How can i download files.. I tried this `sftp.Get(FilePath + FileName , "C://");` But, i end up getting an Exception. – Illep Jun 05 '12 at 17:24
  • Just do this sftp.get(@"PATH TO FILE"); – Botonomous Jun 05 '12 at 17:43
  • No it should be Uppercase `G`et(@"Path to file"); and then i get this exception --> `Tamir.SharSsh.jsch.SftpException`. Any clue how to solve this ? – Illep Jun 06 '12 at 17:36
  • lol i didnt actually mean to write 'path to file' i meant write the full path to the file you want to upload. – Botonomous Jun 06 '12 at 17:59
  • Yes yes... it was `/usr/home/somedirectory`. i did understand that. anyway i get the above error. Actually i am trying to download the file to my local machine. – Illep Jun 06 '12 at 18:28
2

I think the FtpAddress parameter should be without ftp or http, so try the following:

 FileUploadUsingSftp("Some-sftp-site.com", "username", 
                     "password", @"D:\", @"name.txt");
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Bela G
  • 21
  • 1