2014-10-07 13 views
0

我使用SSH.NET從SFTP下載文件,並在這裏是我的代碼下載:文件的空當使用SSH.NET

string host = ConfigurationManager.AppSettings["SFTPDomain"]; 
string username = ConfigurationManager.AppSettings["SFTPUser"]; 
string password = ConfigurationManager.AppSettings["SFTPPass"]; 
string remoteFileName = ConfigurationManager.AppSettings["SFTPFileName"].ToString(); 

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

    using (var file = File.OpenWrite(FilePath)) 
    { 
     sftp.DownloadFile(remoteFileName, file); 
    } 

    sftp.Disconnect(); 
} 

的問題是,這是CSV下載,但它確實該文件裏面沒有任何數據。我也改變了remoteFile路徑,但是文件仍然是用空數據下載的。我試圖檢查文件是否存在使用

if (sftp.Exists(remoteFileName)) 
{ 
} 

即使我改變remoteFileName與"pp"它總是返回true。

任何人都可以幫助我,我做錯了什麼?或者推薦我另一個其他庫從SFTP服務器下載文件。我已經嘗試了WinSCP,但是我得到了hostkey錯誤,所以我試圖按照服務器tutorial的指導通過正確的SshHostKeyFingerprint。我仍然得到hostkey錯誤。是否有任何簡單的庫我只需要從SFTP下載文件?

+0

已經試過sshnet – Coder 2014-10-07 05:51:09

+0

檢查https://sshnet.codeplex.com/discussions/358307 – Mate 2014-10-07 06:09:29

+0

關於WinSCP主機密鑰問題:您鏈接到的教程是關於您的帳戶私鑰,而不是關於服務器(主機)密鑰。請參閱WinSCP文章[我在哪裏可以獲得用於腳本編寫或.NET程序集的SSH主機密鑰指紋?](http://winscp.net/eng/docs/faq_script_hostkey)。請注意,接受任何主機密鑰的SShNet默認行爲是不安全的。使用SshNet,您應該處理'HostKeyReceived'事件並驗證主機密鑰。 WinSCP實施密鑰驗證以維護安全。 – 2014-10-07 06:59:15

回答

0

嘗試使用:

using (Stream file = File.OpenWrite(FilePath)) 
{ 
    sftp.DownloadFile(remoteFileName, file); 
} 

您需要在本地保存之前閱讀從遠程服務器流。

相關問題