C#利⽤SFTP实现上传下载
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本⼈开始尝试使⽤Tamir.SharpSSH.dll但它对新版本的openssh 不⽀持,所有采⽤Ssh.Net⽅式需要依赖:Renci.SshNet.dll
/// <summary>
/// SFTP操作类
/// </summary>
public class SFTPHelper
{
#region 字段或属性
private SftpClient sftp;
/// <summary>
/// SFTP连接状态
/// </summary>
public bool Connected { get { return sftp.IsConnected; } }
#endregion
#region 构造
/// <summary>
/// 构造
/// </summary>
/// <param name="ip">IP</param>
/// <param name="port">端⼝</param>
/// <param name="user">⽤户名</param>
/// <param name="pwd">密码</param>
public SFTPHelper(string ip, string port, string user, string pwd)
{
sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
}
#endregion
#region 连接SFTP
/// <summary>
/// 连接SFTP
/// </summary>
/// <returns>true成功</returns>
public bool Connect()
{
try
{
if (!Connected)
{
sftp.Connect();
}
return true;
}
catch (Exception ex)
{
/
/ TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region 断开SFTP
/// <summary>
/// 断开SFTP
/// </summary>
public void Disconnect()
{
try
{
if (sftp != null && Connected)
{
sftp.Disconnect();
}
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));
throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region SFTP上传⽂件
/// <summary>
/// SFTP上传⽂件
/// </summary>
/// <param name="localPath">本地路径</param>
/// <param name="remotePath">远程路径</param>
public void Put(string localPath, string remotePath)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
sftp.UploadFile(file, remotePath);
Disconnect();
}
connect下载}
catch (Exception ex)
{
/
/ TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP⽂件上传失败,原因:{0}", ex.Message));        throw new Exception(string.Format("SFTP⽂件上传失败,原因:{0}", ex.Message));
}
}
#endregion
#region SFTP获取⽂件
/// <summary>
/// SFTP获取⽂件
/// </summary>
/// <param name="remotePath">远程路径</param>
/// <param name="localPath">本地路径</param>
public void Get(string remotePath, string localPath)
{
try
{
Connect();
var byt = sftp.ReadAllBytes(remotePath);
Disconnect();
File.WriteAllBytes(localPath, byt);
}
catch (Exception ex)
{
/
/ TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP⽂件获取失败,原因:{0}", ex.Message));        throw new Exception(string.Format("SFTP⽂件获取失败,原因:{0}", ex.Message));
}
}
#endregion
#region 删除SFTP⽂件
/// <summary>
/// 删除SFTP⽂件
/// </summary>
/// <param name="remoteFile">远程路径</param>
public void Delete(string remoteFile)
{
try
{
Connect();
sftp.Delete(remoteFile);
Disconnect();
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP⽂件删除失败,原因:{0}", ex.Message));        throw new Exception(string.Format("SFTP⽂件删除失败,原因:{0}", ex.Message));
}
}
#endregion
#region 获取SFTP⽂件列表
/// <summary>
/// 获取SFTP⽂件列表
/// </summary>
/// <param name="remotePath">远程⽬录</param>
/// <param name="fileSuffix">⽂件后缀</param>
/// <returns></returns>
public ArrayList GetFileList(string remotePath, string fileSuffix)
{
try
{
Connect();
var files = sftp.ListDirectory(remotePath);
Disconnect();
var objList = new ArrayList();
foreach (var file in files)
{
string name = file.Name;
if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
{
objList.Add(name);
}
}
return objList;
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP⽂件列表获取失败,原因:{0}", ex.Message));        throw new Exception(string.Format("SFTP⽂件列表获取失败,原因:{0}", ex.Message));
}
}
#endregion
#region 移动SFTP⽂件
/// <summary>
/// 移动SFTP⽂件
/// </summary>
/// <param name="oldRemotePath">旧远程路径</param>
/// <param name="newRemotePath">新远程路径</param>
public void Move(string oldRemotePath, string newRemotePath)
{
try
{
Connect();
sftp.RenameFile(oldRemotePath, newRemotePath);
Disconnect();
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP⽂件移动失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP⽂件移动失败,原因:{0}", ex.Message));
}
}
#endregion
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。