首先在Nuget package下载第三方dll:
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System;
using System.IO;
namespace Test
{
public class SFTPHelper
{
private SftpClient sftp;
public bool Connected {
get {
return sftp.IsConnected; } }
public void InitClient(string server, string port, string user, string pwd)
{
try
{
sftp = new SftpClient(server, Int32.Parse(port), user, pwd);
}
catch (Exception ex)
{
throw ex;
}
}
public bool Connect()
{
try
{
if (!Connected)
{
sftp.Connect();
}
return true;
}
catch (Exception ex)
{
throw ex;
return false;
}
}
public void Disconnect()
{
try
{
if (sftp != null && Connected)
{
sftp.Disconnect();
}
}
catch (Exception ex)
{
throw ex;
}
}
public void