SFTP:SecureFileTransferProtocol的缩写,安全文件传送协议.相对于FTP多了一个加密的过程。
c#中利用SFTP协议上传下载文件,主要是要引用第三方类库Tamir.SharpSSH.dll。
将以下3个dll引用到项目中
1.有两种认证方式:
1)利用password方式:需要用到host,username,password,port。
2)利用private ssh key方式:需要用到host,username,port,privateSshKeyPath。该方式不需要password。
以下是SFTPHelper类,实现了对文件的操作,供参考。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Tamir.SharpSsh; using System.IO; using Tamir.SharpSsh.jsch; namespace SFTPHelper { public class SFTP { private int _port; private string _privateSshKeyPath; private Sftp _tamirSftp; public SFTP(string host, string username, string password) { this._tamirSftp = new Sftp(host, username, password); this._port = 22; } public SFTP(string host, int port, string username, string password) { this._tamirSftp = new Sftp(host, username, password); this._port = port; } public SFTP(string host, int port, string username, string password, string privateSshKeyPath) { this._port = port; if (string.IsNullOrEmpty(privateSshKeyPath)) { this._tamirSftp = new Sftp(host, username, password); } else { this._tamirSftp = new Sftp(host, username); this._privateSshKeyPath = privateSshKeyPath; } } //连接SFTP private void Connect() { if (!string.IsNullOrEmpty(this._privateSshKeyPath)) { this._tamirSftp.AddIdentityFile(this._privateSshKeyPath); } this._tamirSftp.Connect(this._port); } //上传多个文件 public bool UploadFiles(string[] files, string toRemotePath) { if (!this._tamirSftp.Connected) { this.Connect(); } try { this._tamirSftp.Put(files, toRemotePath); return true; } catch (Exception ex) { return false; } finally { this._tamirSftp.Close(); } } //上传单个文件 public bool UploadFile(string file, string toRemotePath) { if (!this._tamirSftp.Connected) { this.Connect(); } try { this._tamirSftp.Put(file, toRemotePath); return true; } catch (Exception ex) { return false; } finally { this._tamirSftp.Close(); } } //下载多个文件到本地路径 public bool DownloadFiles(string[] files, string toLocalPath) { if (!this._tamirSftp.Connected) { this.Connect(); } try { if (!Directory.Exists(toLocalPath)) { Directory.CreateDirectory(toLocalPath); } this._tamirSftp.Get(files, toLocalPath); return true; } catch (Exception ex) { return false; } finally { this._tamirSftp.Close(); } } //下载单个文件到本地路径 public bool DownloadFile(string file, string toLocalPath) { if (!this._tamirSftp.Connected) { this.Connect(); } try { this._tamirSftp.Get(file, toLocalPath); return true; } catch (Exception ex) { throw ex; // return false; } finally { this._tamirSftp.Close(); } }
//遍历远程文件夹public string TryConnect() { var errorMessage = ""; try { this.Connect(); } catch(Exception ex) { errorMessage = ex.Message; } finally { if (!this._tamirSftp.Connected) { this._tamirSftp.Close(); } } return errorMessage; }
public string[] ListFiles(string remotePath)
{
if (!this._tamirSftp.Connected) { this.Connect(); }
string[] files = new string[] { };
try
{
files = this._tamirSftp.GetFileList(remotePath).Cast<string>().ToArray();
}
catch (Exception ex)
{ }
finally
{
this._tamirSftp.Close(); } return files;
}
}
} 两篇其他人写的比较不错的博客,可以借鉴一下:http://blog.csdn.net/xylinzai_xy/article/details/11928519
http://blog.csdn.net/haiyangzhibing/article/details/52678032