不多说,贴代码,看不懂得可以留言。需要引入WinSCP

 public class WebWinScp
{
//远程上传路径
private SessionOptions sessionOptions = null;
private WinSCP.Session session = new WinSCP.Session();
/// <summary>
///
/// </summary>
/// <param name="HostName">FTP服务器</param>
/// <param name="UserName">账号</param>
/// <param name="Password">密码</param>
/// <param name="SshPrivateKeyPath">Ssh私有密钥路径(以PPK结尾的文件)</param>
public WebWinScp(string HostName, string UserName, string Password, string SshPrivateKeyPath)
{
sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = HostName,
UserName = UserName,
Password = Password,
SshPrivateKeyPath = SshPrivateKeyPath,
GiveUpSecurityAndAcceptAnySshHostKey = true
};
}
/// <summary>
/// 下载FTP上的所有文件,并移除FTP上下载的问题
/// </summary>
/// <param name="localPath">本地下载路径</param>
/// <param name="remoteDoPath">远程下载路径</param>
public void Download(string localPath, string remoteDoPath)
{
try
{
//开启连接
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
//传输模式 二进制
transferOptions.TransferMode = TransferMode.Binary;
//FTP上的目录列表
RemoteDirectoryInfo directoryInfo = session.ListDirectory(remoteDoPath);
RemoteFileInfoCollection ct = directoryInfo.Files;
TransferOperationResult transferResult;
foreach (RemoteFileInfo item in ct)
{
transferResult = session.GetFiles(remoteDoPath + item.Name, localPath, true, transferOptions);
if (transferResult != null)
{
transferResult.Check();
}
}
}
catch (Exception)
{
throw;
}
finally
{
if (session.Opened)
{
session.Close();
}
}
}
/// <summary>
/// 上传本地文件到FTP上,上传成功删除本机文件
/// </summary>
/// <param name="localPath">本地上传路径</param>
/// <param name="remoteUpPath">远程上传路径</param>
public void Upload(string localPath, string remoteUpPath)
{
try
{
//开启连接
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
//传输模式 二进制
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult = null;
transferResult = session.PutFiles(localPath, remoteUpPath, true, transferOptions);
if (transferResult != null)
{
transferResult.Check();
}
}
catch (Exception)
{
throw;
}
finally
{
if (session.Opened)
{
session.Close();
}
}
}
}
05-11 09:39