2012-06-05 136 views
1

我需要知道一種通過SFTP連接到FTP站點的方法。我正在使用SharpSSH,我無法找到執行該程序的示例。上傳使用SFTP從FTP站點下載文件

現在,我已經下載了SharpSSH.DLL文件並添加爲參考。現在我需要編寫可以連接的代碼,並從FTP服務器上載/下載文件。

我該怎麼做?幫幫我。

UPDATE enter image description here

代碼:

//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"); 

上面的代碼中的主要方法。

然後;

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(); 
       } 
      } 
     } 
+2

它是FTP站點,還是SFTP站點或FTPS站點?他們是3種不同的東西。 – Joe

+0

這是一個'SFTP'網站 – Illep

+0

這似乎是DNS解析的問題。您的計算機能否成功將您的SFTP服務器的名稱解析爲IP地址? – Joe

回答

2

你現在做了什麼?

我們不能只給你'如何上傳文件的一個直接的答案....

這裏是一個教程: http://saravanandorai.blogspot.com/2012/01/sftp-and-file-upload-in-sftp-using-c.html

+0

首先,我需要建立到SFTP服務器的連接。然後將文件下載到本地目錄 – Illep

+0

看看我提供的教程。它擁有你所要求的一切。 – Botonomous

+0

我已更新我的問題。根據教程,我做了一個例外。你能幫我解決這個 ? – Illep

2

我認爲FtpAddress參數應該是沒有ftphttp,因此請嘗試以下操作:

FileUploadUsingSftp("Some-sftp-site.com", "username", 
        "password", @"D:\", @"name.txt");