c#winforms程序 SFTP协议 上传/下载文件


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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值